2026-05-23 10:37:43 +00:00
|
|
|
from Strategies.strat import PathFindingStrategy
|
2026-05-25 10:45:18 +00:00
|
|
|
from Strategies.path import restore
|
2026-05-23 10:37:43 +00:00
|
|
|
from collections import deque
|
|
|
|
|
|
|
|
|
|
class BFS(PathFindingStrategy):
|
2026-05-25 13:02:23 +00:00
|
|
|
def findPath(self, maze, start, exit):
|
|
|
|
|
if exit is None:
|
|
|
|
|
return [], 0
|
|
|
|
|
|
2026-05-23 10:37:43 +00:00
|
|
|
queue = deque([start])
|
|
|
|
|
visited = {start}
|
|
|
|
|
parent = {}
|
|
|
|
|
|
|
|
|
|
while queue:
|
|
|
|
|
current = queue.popleft()
|
|
|
|
|
|
|
|
|
|
if current == exit:
|
|
|
|
|
break
|
|
|
|
|
|
2026-05-25 10:45:18 +00:00
|
|
|
for n in maze.getNeighbors(current):
|
2026-05-23 10:37:43 +00:00
|
|
|
if n not in visited:
|
|
|
|
|
visited.add(n)
|
|
|
|
|
parent[n] = current
|
2026-05-25 13:02:23 +00:00
|
|
|
queue.append(n)
|
2026-05-23 10:37:43 +00:00
|
|
|
|
2026-05-25 10:45:18 +00:00
|
|
|
return restore(parent, start, exit), len(visited)
|