簡體   English   中英

康威的游戲鄰居計數

[英]Conway's game of life neighbor count

def neighbors(matrix, r, c):

    live_neighbors = 0

    if matrix[r][c-1] != 0:
        live_neighbors += 1
    if matrix[r-1][c] != 0:
        live_neighbors += 1
    if matrix[r-1][c+1] != 0:
        live_neighbors += 1
    if matrix[r][c-1] != 0:
        live_neighbors += 1
    if matrix[r][c+1] != 0:
        live_neighbors += 1
    if matrix[r+1][c-1] != 0:
        live_neighbors += 1
    if matrix[r+1][c] != 0:
        live_neighbors += 1
    if matrix[r+1][c+1] != 0:
        live_neighbors += 1

    return live_neighbors

這是我到目前為止編寫的代碼。 如何計算邊界單元格的鄰居? 如果我使用這個代碼,我會得到一個索引超出范圍的錯誤。

您可以使用輔助函數來檢查邊界:

def neighbors(matrix, r, c):
    def get(r, c):
        return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c]

    live_neighbors = 0

    if get(r, c-1) != 0:
        live_neighbors += 1
    if get(r-1, c) != 0:
        live_neighbors += 1
    if get(r-1, c+1) != 0:
        live_neighbors += 1
    if get(r, c-1) != 0:
        live_neighbors += 1
    if get(r, c+1) != 0:
        live_neighbors += 1
    if get(r+1, c-1) != 0:
        live_neighbors += 1
    if get(r+1, c) != 0:
        live_neighbors += 1
    if get(r+1, c+1) != 0:
        live_neighbors += 1

    return live_neighbors

您還可以在生成器表達式中使用itertools.product作為sum而不是if語句來計算所有活動鄰居:

from itertools import product
def neighbors(matrix, r, c):
    def get(r, c):
        return 0 <= r < len(matrix) and 0 <= c < len(matrix[r]) and matrix[r][c]
    return sum(get(r + i, c + j) for i, j in product(range(-1, 2), 2) if i or j)

沒有所有if語句的可能解決方案:

def neighbors(matrix, r, c):
    def get(r, c):
        if 0 <= r < len(matrix) and 0 <= c < len(matrix[r]):
            return matrix[r][c]
        else:
            return 0

    neighbors_list = [get(r-1, c-1), get(r-1, c), get(r-1, c+1),
                      get(r  , c-1),              get(r  , c+1),
                      get(r+1, c-1), get(r+1, c), get(r+1, c+1)]

    return sum(map(bool, neighbors_list))

matrix = [ [0, 0, 0, 0, 0],
           [0, 0, 0, 0, 1],
           [0, 0, 0, 1, 1],
           [0, 0, 0, 1, 1],
           [0, 0, 1, 1, 1] ]

print(neighbors(matrix, 0, 0))  # 0
print(neighbors(matrix, 1, 2))  # 1
print(neighbors(matrix, 3, 2))  # 4
print(neighbors(matrix, 4, 4))  # 3

如果單元格只有0或1的值,則neighbors函數將簡單地返回sum(neighbors_list)

最接近你的是:

def neighbors(matrix, r, c):

live_neighbors = 0

if c and matrix[r][c-1] != 0:
    live_neighbors += 1
if r and matrix[r-1][c] != 0:
    live_neighbors += 1
if r and matrix[r-1][c+1] != 0:
    live_neighbors += 1
if c and matrix[r][c-1] != 0:
    live_neighbors += 1
if c < len(matrix[r])-1 and matrix[r][c+1] != 0:
    live_neighbors += 1
if r < len(matrix)-1 and matrix[r+1][c-1] != 0:
    live_neighbors += 1
if r < len(matrix)-1 and matrix[r+1][c] != 0:
    live_neighbors += 1
if r < len(matrix)-1 and matrix[r+1][c+1] != 0:
    live_neighbors += 1

return live_neighbors

等等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM