繁体   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