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 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 cocould use to fabricate 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.
Original question link: http://www.checkio.org/mission/the-square-chest/
Question: calculate the number of squares
Idea: recursive search; in fact, the question is limited to 16 points, so the total number of squares is 14. This observation is as follows: [6, 7, 8, 10, 12, 14, 15, 16] square in Figure 1, [7, 8, 10, 11, 12, 14, 15, 16] non-Square
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
Code idea: first, recursively enumerate closed graphs with loops and number of edges multiples of 4. Finally, match closed graphs by storing the square numbers in advance to obtain the number of squares in the graph.