Unnest the queue used for path finding
This commit is contained in:
parent
bb3d4158ca
commit
b70c00eb8d
|
@ -460,31 +460,22 @@ class Maze:
|
||||||
self.stepx = self.stepy = 0
|
self.stepx = self.stepy = 0
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# Forest Fire algorithm with step count
|
# Forest Fire algorithm
|
||||||
queue = [[(self.destx, self.desty)]]
|
queue, visited = deque([(self.destx, self.desty)]), set()
|
||||||
visited, count, distance = set(), 1, 0
|
while queue:
|
||||||
while count:
|
x, y = queue.pop()
|
||||||
if not queue[distance]: distance += 1
|
|
||||||
x, y = queue[distance].pop()
|
|
||||||
count -= 1
|
|
||||||
if (x, y) not in visited:
|
if (x, y) not in visited:
|
||||||
visited.add((x, y))
|
visited.add((x, y))
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
dx, dy = MIDDLE - x, MIDDLE - y
|
dx, dy = MIDDLE - x, MIDDLE - y
|
||||||
if dx**2 + dy**2 <= 2:
|
if dx**2 + dy**2 <= 2:
|
||||||
|
# Succeeded on finding a path
|
||||||
self.stepx, self.stepy = dx, dy
|
self.stepx, self.stepy = dx, dy
|
||||||
return False
|
return False
|
||||||
for i, j in around(x, y):
|
for i, j in around(x, y):
|
||||||
if self.map[i][j] == EMPTY and check(i, j):
|
if self.map[i][j] == EMPTY and check(i, j):
|
||||||
try:
|
queue.appendleft((i, j))
|
||||||
queue[distance + 1].append((i, j))
|
|
||||||
except IndexError:
|
|
||||||
queue.append([(i, j)])
|
|
||||||
finally:
|
|
||||||
count += 1
|
|
||||||
|
|
||||||
# Failed to find way to move to target
|
# Failed to find way to move to target
|
||||||
self.stepx = self.stepy = 0
|
self.stepx = self.stepy = 0
|
||||||
return True
|
return True
|
||||||
|
|
Loading…
Reference in New Issue