2015-04-03 43 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

相關問題