44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
from strategy import PathFindingStrategy
|
|
from Maze import Maze, Cell
|
|
|
|
|
|
class DeikstraFind(PathFindingStrategy):
|
|
def findPath(maze, start, exit):
|
|
|
|
if not start or not exit:
|
|
return []
|
|
|
|
queue = [start]
|
|
|
|
distances = {start: 0}
|
|
parents = {start: None}
|
|
|
|
while len(queue) != 0:
|
|
best_cell = queue[0]
|
|
for cell in queue:
|
|
if distances[cell] < distances[best_cell]:
|
|
best_cell = cell
|
|
|
|
u = best_cell
|
|
queue.remove(u)
|
|
|
|
if u == exit:
|
|
path = []
|
|
current = exit
|
|
while current is not None:
|
|
path.append(current)
|
|
current = parents[current]
|
|
path.reverse()
|
|
return path, len(parents)
|
|
|
|
for child in maze.getNeighbors(u):
|
|
distance_through_u = distances[u] + 1
|
|
|
|
if distance_through_u < distances.get(child, float('inf')):
|
|
distances[child] = distance_through_u
|
|
parents[child] = u
|
|
if child not in queue:
|
|
queue.append(child)
|
|
|
|
return []
|