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.
[['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'.
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']]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.