博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
The square chest
阅读量:4561 次
发布时间:2019-06-08

本文共 4947 字,大约阅读时间需要 16 分钟。

The square chest

Sophia pressed the button in front of her, slamming her fist against it. The door rumbled and appeared to do nothing.

“Oh man, that’s not very interesting at all.” Sofia complained.

Suddenly, the the door hissed and a jet of ice cold steam billowed out. The door began moving out the wall as if it were pushed from the inside. Sofia and the other two stepped back, watching their feet. The wall finally stopped moving and revealed itself to be a large chest of some sort.

“Oh man, could it be treasure?” Sofia studied the chest intently.

Nikola poked his head through the doorway where the chest had just exited. “Well, there’s nothing in there but darkness.” He turned to the chest which stood several feet taller than him and began studying it himself. He noticed some carvings on the side. “Hey, this looks like a manifest of some sort. Or a contents label. Look, there Ag, Na, Li, Mg... all raw materials we could use to fabricate the parts we need to fix the ship!”

“There’s a keypad on it though... It’s locked.” Sofia said with a frustrated sigh.

“No problem, we can solve it. Let’s take a look!”

On the chest keypad is a grid of numbered dots. The grid is comprised of a square shaped array of dots and contains lines that connect some pairs of adjacent dots. The answer to the code is the number of squares that are formed by these lines. For example, in the figure shown below, there are 3 squares: 2 small squares and 1 medium square.

The dots are marked by the numbers 1 through 16. The endpoints of the lines are represented by lists of two numbers.

Input: A list of lines as a list of list. Each list consists of the two integers.

Output: The quantity of squares formed as an integer.

原题链接: 

题目大义: 计算正方形的个数

思路: 递归搜索; 实际上因为, 题目限定有16个点, 因此总的正方形个数为14个, 这个观察在处理图一中如[6,7,8,10,12,14,15,16]正方形, [7,8,10,11,12,14,15,16]非正方形时起到作用

1 squares = [[1,2,5,6], [2,3,6,7], [3,4,7,8], [5,6,9,10], [6,7,10,11],[7,8,11,12],[9,10,13,14],[10,11,14,15],[11,12,15,16], 2                     [1,2,3,5,7,9,10,11], [2,3,4,6,8,10,11,12], [5,6,7,9,11,13,14,15],[6,7,8,10,12,14,15,16], 3                     [1,2,3,4,5,8,9,12,13,14,15,16]] 4  5 def line_joint(node, lines_list, square_nodes, square_lines, lines_node): 6     lines_node.append(node) 7  8     for each in lines_list: 9         if each not in square_lines and node in each:10             square_lines.append(each)11 12             adjnode = 013             if each[0] == node:14                 adjnode = each[1]15             else:16                 adjnode = each[0]17 18             if adjnode == square_lines[0][0]: #link to the first node19                 if len(square_lines) % 4 == 0: #may be a squre20                     sorted_lines_node = sorted(lines_node)21                     rep = False22                     for pre_square in square_nodes:23                         if pre_square == sorted_lines_node:24                             rep = True25                             break26 27                     if rep == False:28                         square_nodes.append(sorted_lines_node)29             else:30                 if adjnode not in lines_node: #not a cycle31                     line_joint(adjnode, lines_list, square_nodes, square_lines, lines_node)32 33             square_lines.pop()34 35     lines_node.pop()36 37 def checkio(lines_list):38     """Return the quantity of squares"""39     square_lines = []40     lines_node = []41     square_nodes = []42 43     for i in range(0, 17):44         line_joint(i, lines_list, square_nodes, square_lines, lines_node)45 46     square_count = 047     for maybe_square in square_nodes:48         for formal_square in squares:49             if maybe_square == formal_square:50                 square_count += 151                 break52 53     return square_count

代码思路, 首先递归枚举出, 环形及边数是4的倍数的闭合图形, 最后通过事先存储好的正方形数字, 去匹配闭合图形, 得到图中的正方形数

review nubatamax codes

1 #anti-clock 2 #l1 = [(n, n + 4, n + 5, n + 1) for n in (1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15)] 3 l1 = [(n, n + 4, n + 5, n + 1) for n in (1, 2, 3, 5, 6, 7, 9, 10, 11)] 4 l2 = [(n, n + 4, n + 8, n + 9, n + 10, n + 6, n + 2, n + 1) for n in (1, 2, 5, 6)] 5 l3 = [(1, 5, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2)] 6  7 #construct lines of squere  8 def seg_gen(path): 9     l = len(path)10     for i in range(l):11         yield sorted((path[i], path[(i + 1) % l])) #each time return a line of this squre12 13 def checkio(lines_list):14     segments = [sorted(segment) for segment in lines_list] #segment is like this form [little, big]15     count = 016     for square_list in (l1, l2, l3):17         for square in square_list:18             if all([seg in segments for seg in seg_gen(square)]): #if the square line all in given lines_list then that's a square19                 count += 120 21     return count

我将第二行代码中的产生l1列表(原代码), 写成了第三行的形式, 加入了不少注释; 该代码的思路是, 枚举所有可能出现的正方形, 如果该正方形的边全在给定的lines_list中, 则正方形数+1.

转载于:https://www.cnblogs.com/hzhesi/p/3910529.html

你可能感兴趣的文章
前端css常用class命名id命名
查看>>
领扣(LeetCode)两句话中的不常见单词 个人题解
查看>>
nginx的location匹配
查看>>
例5-11和例5-12和例5-13
查看>>
无法搜到个例网络
查看>>
利用CSS3制作网页动画
查看>>
熟悉LINUX系统的基本操作
查看>>
[LeetCode] 42. Trapping Rain Water
查看>>
bzoj 2553: [BeiJing2011]禁忌 AC自动机+矩阵乘法
查看>>
python 全栈开发,Day80(博客系统分析,博客主页展示)
查看>>
手机QQ会员H5加速方案——sonic技术内幕
查看>>
第四周
查看>>
python中的单元测试pyUnit & what is unit test and why
查看>>
c++:资源管理(RAII)、new/delete的使用、接口设计与声明、swap函数
查看>>
第四周作业
查看>>
函数对象
查看>>
leetcode x 的平方根 python
查看>>
java多态
查看>>
单例模式
查看>>
P1630 求和
查看>>