简体   繁体   English

Python“传播”模拟

[英]Python 'spread' simulation

So, I have a list in the form of 所以,我有一个列表形式

[['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']]

and I want the '0' surrounding the '1' to change to a '1' after a step , like this. 我希望围绕“ 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']]

and after enough steps, all the '0' become '1'. 经过足够的步骤后,所有的“ 0”变为“ 1”。

The code I have is as follows 我的代码如下

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)

but it seems the 'spread' far too much for 1 step. 但似乎“传播”太多了,只有一步之遥。 I can't seem to understand why but I guess it's due to this line 我似乎不明白为什么,但是我想这是由于这条线

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)

Would really appreciate if someone could guide me regarding this code. 如果有人可以为我提供有关此代码的指导,我将不胜感激。

The problem is that you're making the newly-spreaded fire part of the original fire in the same matrix and so they would further spread into other bushes as you continue searching for fire. 问题在于,您正在将新传播的火力以相同矩阵的形式作为原始火力的一部分,因此当您继续寻找火力时,它们会进一步扩散到其他灌木丛中。 You should use a separate list to keep track of the new fires and only apply them in the end when you have finished scanning the entire bushfire matrix: 您应该使用一个单独的列表来跟踪新的火灾,并且仅在完成对整个林区火灾矩阵的扫描后才将它们应用于最后:

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)

so that: 以便:

simulate_bushfire(l, 1)

would return: 会返回:

[['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