簡體   English   中英

Python“傳播”模擬

[英]Python 'spread' simulation

所以,我有一個列表形式

[['0','0','0','0','0'],
['0','0','0','0','0'],
['1','0','0','0','0'],
['1','0','0','0','0'],
['0','0','0','0','0']]

我希望圍繞“ 1”的“ 0”在一步之后變為“ 1”,就像這樣。

[['0','0','0','0','0'],
['1','0','0','0','0'],
['1','1','0','0','0'],
['1','1','0','0','0'],
['1','0','0','0','0']]

經過足夠的步驟后,所有的“ 0”變為“ 1”。

我的代碼如下

def simulate_bushfire(list, steps):
    for _ in range(steps):# iterates through the number of steps 
        for y in range(len(initial_bushfire[0])):
            for x in range(len(initial_bushfire)): #for all y coordinates possible in the file   
                if initial_bushfire[y][x] =='1':# looks for cells that has '1' in it 
                    for a in range(x-1,x+2): #looks at the neighbour of the cell that has'1' in it (x coordinates)
                        for b in range(y-1,y+2):#looks at the neighbour of the cell that has'1' in it (y coordinates)                           
                            if a<0 or b<0 or b>=len(initial_bushfire[0]) or a>=len(initial_bushfire):# if neighbour is outside the border of the map, 
                                #code will ignore to avoid errors like list out of range 
                                continue
                            if initial_bushfire[b][a]=='':# if there's an empty string (no tree)
                                continue    # ignore this as well (no trees to burn )
                            if initial_bushfire[b][a]=='0': #if there is a '0' in the file (there is a tree)
                                initial_bushfire[b][a]='1'# change the '0' to a '1' (tree on fire)
    return (initial_bushfire)

但似乎“傳播”太多了,只有一步之遙。 我似乎不明白為什么,但是我想這是由於這條線

for a in range(x-1,x+2): #looks at the neighbour of the cell that has'1' in it (x coordinates)
    for b in range(y-1,y+2):#looks at the neighbour of the cell that has'1' in it (y coordinates)

如果有人可以為我提供有關此代碼的指導,我將不勝感激。

問題在於,您正在將新傳播的火力以相同矩陣的形式作為原始火力的一部分,因此當您繼續尋找火力時,它們會進一步擴散到其他灌木叢中。 您應該使用一個單獨的列表來跟蹤新的火災,並且僅在完成對整個林區火災矩陣的掃描后才將它們應用於最后:

def simulate_bushfire(initial_bushfire, steps):
    for _ in range(steps):# iterates through the number of steps
        new_fire = []
        for y in range(len(initial_bushfire[0])):
            for x in range(len(initial_bushfire)): #for all y coordinates possible in the file
                if initial_bushfire[y][x] =='1':# looks for cells that has '1' in it
                    for a, b in (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1): #looks at the neighbouring cells
                        if a<0 or b<0 or b>=len(initial_bushfire[0]) or a>=len(initial_bushfire):# if neighbour is outside the border of the map,
                            #code will ignore to avoid errors like list out of range
                            continue
                        if initial_bushfire[b][a]=='':# if there's an empty string (no tree)
                            continue    # ignore this as well (no trees to burn )
                        if initial_bushfire[b][a]=='0': #if there is a '0' in the file (there is a tree)
                            new_fire.append((b, a))# change the '0' to a '1' (tree on fire)
        for y, x in new_fire:
            initial_bushfire[y][x] = '1'
    return (initial_bushfire)

以便:

simulate_bushfire(l, 1)

會返回:

[['0', '0', '0', '0', '0'],
 ['1', '0', '0', '0', '0'],
 ['1', '1', '0', '0', '0'],
 ['1', '1', '0', '0', '0'],
 ['1', '0', '0', '0', '0']]

暫無
暫無

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

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