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.

原题链接: http://www.checkio.org/mission/the-square-chest/

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

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

 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],
[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],
[1,2,3,4,5,8,9,12,13,14,15,16]] def line_joint(node, lines_list, square_nodes, square_lines, lines_node):
lines_node.append(node) for each in lines_list:
if each not in square_lines and node in each:
square_lines.append(each) adjnode = 0
if each[0] == node:
adjnode = each[1]
else:
adjnode = each[0] if adjnode == square_lines[0][0]: #link to the first node
if len(square_lines) % 4 == 0: #may be a squre
sorted_lines_node = sorted(lines_node)
rep = False
for pre_square in square_nodes:
if pre_square == sorted_lines_node:
rep = True
break if rep == False:
square_nodes.append(sorted_lines_node)
else:
if adjnode not in lines_node: #not a cycle
line_joint(adjnode, lines_list, square_nodes, square_lines, lines_node) square_lines.pop() lines_node.pop() def checkio(lines_list):
"""Return the quantity of squares"""
square_lines = []
lines_node = []
square_nodes = [] for i in range(0, 17):
line_joint(i, lines_list, square_nodes, square_lines, lines_node) square_count = 0
for maybe_square in square_nodes:
for formal_square in squares:
if maybe_square == formal_square:
square_count += 1
break return square_count

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

review nubatamax codes

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

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

The square chest的更多相关文章

  1. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  2. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  3. [LeetCode] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  4. [LeetCode] Maximal Square 最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  5. OPEN CASCADE Gauss Least Square

    OPEN CASCADE Gauss Least Square eryar@163.com Abstract. The least square can be used to solve a set ...

  6. OpenCascade Eigenvalues and Eigenvectors of Square Matrix

    OpenCascade Eigenvalues and Eigenvectors of Square Matrix eryar@163.com Abstract. OpenCascade use th ...

  7. Leetcode: Matchsticks to Square && Grammar: reverse an primative array

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  8. Leetcode: Valid Word Square

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  9. Modified Least Square Method and Ransan Method to Fit Circle from Data

    In OpenCv, it only provide the function fitEllipse to fit Ellipse, but doesn't provide function to f ...

随机推荐

  1. Android应用程序插件化研究之AssertManager

    最近在研究Android应用的插件化开发,看了好几个相关的开源项目.插件化都是在解决以下几个问题: 如何把插件apk中的代码和资源加载到当前虚拟机. 如何把插件apk中的四大组件注册到进程中. 如何防 ...

  2. AzCopy – 上传/下载 Windows Azure Blob 文件

    在我们收到的请求中,有一个频繁出现的请求是提供一种能在 Windows Azure Blob 存储与其本地文件系统之间轻松上传或下载文件的方法.一年半前, 我们很高兴地发布了 AzCopy, Wind ...

  3. 【转】Android 之 下拉框(Spinner)的使用

    原文网址:http://imshare.iteye.com/blog/770950 下拉列表 Spinner. Spinner的使用,可以极大提高用户的体验性.当需要用户选择的时候,可以提供一个下拉列 ...

  4. Jquery回车键切换焦点方法(兼容各大浏览器)

    做项目时,客户要求能够用enter回车直接切换输入(焦点),当最后一个时候,直接提交信息. 第一想法就是,网上去copy一段代码直接用.但了百度.谷歌找了个遍,找到的代码80%以上都是一样的.有的代码 ...

  5. hdu1540-Tunnel Warfare (线段树区间合并)

    题意:n个村庄,有三种操作,D x 破坏位置为x的村庄,R 修复上一次被破坏的村庄,Q x 输出含有x村庄的连续村庄的最大个数.线段树搞之,区间合并. ls[maxn]为当前节点左面的连续区间,rs[ ...

  6. spring 配置文件XSD地址

    这边部署不能访问外网,所以sping配置文件里的XSD地址要改一下象  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd ...

  7. Hive 6、Hive DML(Data Manipulation Language)

    DML主要是对Hive 表中的数据进行操作的(增 删 改),但是由于Hadoop的特性,所以单条的修改.删除,其性能会非常的低所以不支持进行级操作: 主要说明一下最常用的批量插入数据较为常用的方法: ...

  8. Cannot find class in classpath 报错

    删除项目文件夹下的target文件夹里面内容,重新运行测试代码,报错 org.testng.TestNGException: Cannot find class in classpath: com.f ...

  9. Wince修改系统时间问题

           当我们需要修改到系统时间的时候,需要用到下面四个函数:SetLoaclTime,GetLocalTime,SetSystemTime,GetSystemTime.这四个函数是用来修改或者 ...

  10. [转]myeclipse 生成JAR包并引入第三方包

    myeclipse 生成JAR包并引入第三方包 我用的是myeclipse8.0 首先用myeclipse生成JAR 一.生成JAR包 1.点选项目右键—>Export 2.Java—>J ...