Python - Multiple if statement get ignored -
i'm implementing bfs on 2d-array list serve "queue". add each unvisited neighbor of current cell(i, j) queue, , in each loop, pop head of queue current cell, until queue empty. standard stuff.
the problem seems 1 "if" statement executed in each loop. can't see why happens.
for tgroup in targets.keys(): #group of targets t in targets[tgroup]: #each target in group visited = [[false]*len(cells[0])]*len(cells) queue = [] cur = none queue.append(t) visited[t[0]][t[1]] = true cells[t[0]][t[1]].fields[tgroup-3] = 0 while len(queue) > 0: cur = queue[0] queue = queue[1:] if cur[0] > 0 , visited[cur[0]-1][cur[1]] false: queue.append((cur[0]-1,cur[1])) visited[cur[0]-1][cur[1]] = true cells[cur[0]-1][cur[1]].fields[tgroup-3] = min(cells[cur[0]-1][cur[1]].fields[tgroup-3], cells[cur[0]][cur[1]].fields[tgroup-3]+1) print 't', cur if cur[0] < len(cells)-1 , visited[cur[0]+1][cur[1]] false: queue.append((cur[0]+1,cur[1])) visited[cur[0]+1][cur[1]] = true cells[cur[0]+1][cur[1]].fields[tgroup-3] = min(cells[cur[0]+1][cur[1]].fields[tgroup-3], cells[cur[0]][cur[1]].fields[tgroup-3]+1) print 'b', cur if cur[1] > 0 , visited[cur[0]][cur[1]-1] false: queue.append((cur[0],cur[1]-1)) visited[cur[0]][cur[1]-1] = true cells[cur[0]][cur[1]-1].fields[tgroup-3] = min(cells[cur[0]][cur[1]-1].fields[tgroup-3], cells[cur[0]][cur[1]].fields[tgroup-3]+1) print 'l', cur if cur[1] < len(cells[0])-1 , visited[cur[0]][cur[1]+1] false: queue.append((cur[0],cur[1]+1)) visited[cur[0]][cur[1]+1] = true cells[cur[0]][cur[1]+1].fields[tgroup-3] = min(cells[cur[0]][cur[1]+1].fields[tgroup-3], cells[cur[0]][cur[1]].fields[tgroup-3]+1) print 'r', cur if cur[0] > 0 , cur[1] > 0 , visited[cur[0]-1][cur[1]-1] false: queue.append((cur[0]-1,cur[1]-1)) visited[cur[0]-1][cur[1]-1] = true cells[cur[0]-1][cur[1]-1].fields[tgroup-3] = min(cells[cur[0]-1][cur[1]-1].fields[tgroup-3], cells[cur[0]][cur[1]].fields[tgroup-3]+1) print 'tl', cur if cur[0] > 0 , cur[1] < len(cells[0])-1 , visited[cur[0]-1][cur[1]+1] false: queue.append((cur[0]-1,cur[1]+1)) visited[cur[0]-1][cur[1]+1] = true cells[cur[0]-1][cur[1]+1].fields[tgroup-3] = min(cells[cur[0]-1][cur[1]+1].fields[tgroup-3], cells[cur[0]][cur[1]].fields[tgroup-3]+1) print 'tr', cur if cur[0] < len(cells)-1 , cur[1] > 0 , visited[cur[0]+1][cur[1]-1] false: queue.append((cur[0]+1,cur[1]-1)) visited[cur[0]+1][cur[1]-1] = true cells[cur[0]+1][cur[1]-1].fields[tgroup-3] = min(cells[cur[0]+1][cur[1]-1].fields[tgroup-3], cells[cur[0]][cur[1]].fields[tgroup-3]+1) print 'bl', cur if cur[0] < len(cells)-1 , cur[1] < len(cells[0])-1 , visited[cur[0]+1][cur[1]+1] false: queue.append((cur[0]+1,cur[1]+1)) visited[cur[0]+1][cur[1]+1] = true cells[cur[0]+1][cur[1]+1].fields[tgroup-3] = min(cells[cur[0]+1][cur[1]+1].fields[tgroup-3], cells[cur[0]][cur[1]].fields[tgroup-3]+1) print 'br', cur
targets
starting coordinates of algorithm. need traverse entire map fill out 'floor field' find shortest path each location of map these targets. each t
in target
stored tuple (i,j)
visited
markers visited nodes.
cur
current node, stored tuple (i,j)
.
cells
2d-array. each cell has list attribute .fields
containing "floor field" weight each target, representing distance cell target. weight of non-target cells default 9, now, simplicity. weight targets default 0.
here's sample 9*9 input map:
4 1 1 1 1 1 1 1 1 0 1 1 0 0 0 0 1 0 0 1 1 1 1 2 0 1 0 0 1 1 1 1 2 0 1 0 0 1 1 0 0 0 0 1 0 3 1 1 0 0 0 1 1 0 3 1 1 0 0 0 1 1 0 0 1 1 0 0 0 1 0 0 0 5 5 5 0 0 1 0 0
but floor field generated target represented number 3
is:
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
it doesn't traverse nodes.
update:
it works when replaced
visited = [[false]*len(cells[0])]*len(cells)
with
visited = [[false x in range(len(cells[0]))] y in range(len(cells))]
can explain difference? lot!
after first call
visited = [[false]*len(cells[0])]*len(cells)
visited
contains len(cells)
number of lists same (they reference 1 list). when change value in there, of gets changed since same.
>>>a = [[false]*4]*4 >>>a[0][0] = true >>>a >>>[[true, false, false, false], [true, false, false, false], [true, false, false, false], [true, false, false, false]] >>> x in a: print id(x) >>> 135345096 135345096 135345096 135345096
on second call, create seperate lists. when change one, value changed in it.
Comments
Post a Comment