【问题标题】:Python 'spread' simulationPython“传播”模拟
【发布时间】:2019-03-20 13:58:13
【问题描述】:

所以,我有一个格式为

的列表
[['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)

但对于 1 步来说,“传播”似乎太多了。我似乎无法理解为什么,但我想这是由于这条线

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)

如果有人能就这段代码指导我,我将不胜感激。

【问题讨论】:

    标签: python indexing list-comprehension


    【解决方案1】:

    问题在于,您正在使新蔓延的火成为同一矩阵中原始火的一部分,因此当您继续寻找火时,它们会进一步蔓延到其他灌木丛中。您应该使用单独的列表来跟踪新的火灾,并且仅在完成整个丛林火灾矩阵的扫描后才应用它们:

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

    【讨论】:

    • 查看报价
    • 你在说什么报价?从 OP 的代码复制的注释中的撇号,SO 的语法解析器无法正确解析?
    • 是的,不知道这是不是故意的,它弄乱了代码的其余部分,看起来像一个字符串
    • 是的,SO 的解析器就是这样的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 2017-11-11
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多