2015-04-03 79 views
2

我要检查,如果■是连续的:如何检查列表中的元素是连续

_|_ _|_1_|_2_|_3_|_ 
_|_1_|_■_|_■_|_ _|_ 
_|_2_|_ _|_ _|_■_|_ 
_|_3_|_ _|_■_|_ _|_ 
_|_4_|_■_|_■_|_ _|_ 
在这种情况下返回True

和例如,如果这样的事情发生了:

_|_ _|_1_|_2_|_3_|_ 
_|_1_|_■_|_■_|_ _|_ 
_|_2_|_ _|_ _|_■_|_ 
_|_3_|_ _|_ _|_ _|_ 
_|_4_|_■_|_■_|_ _|_ 

在这种情况下返回False

我使用的列表如下:

my_list=[[" "," "," "],[" "," "," "],[" "," "," "], 
[" "," "," "]] 

这些数字只在打印电路板时出现,所以我用my_list工作。

+0

你的意思是相邻的?因为它们在第二个例子中已经与另一个盒子相邻。 – Amadan 2015-04-03 04:42:53

+0

对角线是否被认为是相邻的? – 2015-04-03 04:43:05

+0

对不起,我的意思是连续的。 – Guolf3377 2015-04-03 04:46:56

回答

2

我不会给你你可以插入的代码(因为它在我看来像一个编程挑战),但我会告诉你如何解决你的问题。

您需要构建一个图。因此,对于每个黑点,您都有相邻黑点的列表(您在此定义相邻的点)。例如,如果所有对角线都是这样计算的,则对于点(2, 3),您的相邻列表将为:(1, 2), (3, 2)。和你的图形看起来就像

{ 
    (2, 3): {(1, 2), (3, 2)}, 
    ... every other node 
} 

你能想出一个更简单的架构,其中(2, 3)将对应于(2 - 1) * len(matrix row) + (3 - 1) = 5。我正在减去一个,因为我使用零作为起点。

现在,当您有图形时,可以使用connected components的算法并检查是否只有一个这样的组件。如果是,则变为True,否则为false。

您的单个连接组件只是一个BFS或DFS。

5

走曲线图中,和如果访问每一个节点,那么您连接(连续的),例如:

def is_contiguous(grid): 
    items = {(x, y) for x, row in enumerate(grid) for y, f in enumerate(row) if f} 
    directions = [(0, 1), (1, 0), (-1, 0), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)] 
    neighbours = {(x, y): [(x+dx, y+dy) for dx, dy in directions if (x+dx, y+dy) in items] 
        for x, y in items} 

    closed = set() 
    fringe = [next(iter(items))] 
    while fringe: 
     i = fringe.pop() 
     if i in closed: 
      continue 
     closed.add(i) 
     for n in neighbours[i]: 
      fringe.append(n) 

    return items == closed 

>>> is_contiguous([["X", "X", ""], ["", "", "X"], ["", "X", ""], ["X", "X", ""]]) 
True 
>>> is_contiguous([["X", "X", ""], ["", "", "X"], ["", "", ""], ["X", "X", ""]]) 
False 

只要空白瓦片是falsy那么这应该作为是工作,例如[[1, 1, 0], [0, 0, 1], [0, 1, 0], [1, 1, 0]]也将返回True。如果您对空白瓷砖有不同的定义,那么只需更改if f就可以定义items

0

这为我工作:

def findContiguous(myList): 
    rowCount = 0 
    previousRowFound = [] 
    rowsWithItems = []  
    for row in myList: 
     currentRowFound = [] 
     rowCount += 1 
     # Determine if target is in the row 
     if "■" in row: 
      currentRowFound = [i for i, x in enumerate(row) if x == "■"] 
      rowsWithItems.append(rowCount) 

     # Check if there is a cell adjacent or diagonal to previous 
     if len(currentRowFound) != 0 and len(previousRowFound) != 0: 
      for cell in currentRowFound: 
       if not((cell + 1 in previousRowFound) or (cell - 1 in previousRowFound) or (cell in previousRowFound)): 
        return False 

     # Check for blank rows in between rows with item 
     if len(rowsWithItems) > 1 and len(previousRowFound) == 0: 
      if rowsWithItems[-2] != rowCount - 1: 
       print(rowsWithItems) 
       print(rowCount) 
       return False 

     # Move on to next row 
     previousRowFound = currentRowFound 
     first = False 
    return True 
+0

我还没有试图运行这个,但是这不是只检查每个单元是否有邻居?如果有一对相邻的细胞未与其他细胞连接,该怎么办?在OP的测试用例中添加一个3,1的单元格,看看你是否仍然得到False的答案。 – PaulMcG 2015-04-03 05:18:13

+0

是的,我已经完成了这个测试,它仍然会产生一个错误。该函数检查上一行中的相邻邻居,因此在您建议的更改中,第2行和第3行之间的不相交会被检测到并返回错误 – 2015-04-03 05:21:07

+0

第1列和第3列中的两列单元格如何? – PaulMcG 2015-04-03 05:24:39

相关问题