40 lines
962 B
Python
40 lines
962 B
Python
from collections import deque
|
|
|
|
from strategies.pathfinding_strategy import PathFindingStrategy
|
|
|
|
class BFS(PathFindingStrategy):
|
|
|
|
def find_path(self, maze, start_cell, exit_cell):
|
|
|
|
queue = deque([start_cell])
|
|
|
|
parents = {start_cell: None}
|
|
visited = {start_cell}
|
|
|
|
visited_count = 0
|
|
|
|
while queue:
|
|
|
|
current = queue.popleft()
|
|
visited_count += 1
|
|
|
|
if current == exit_cell:
|
|
path = []
|
|
|
|
while current is not None:
|
|
path.append(current)
|
|
current = parents[current]
|
|
|
|
path.reverse()
|
|
return path, visited_count
|
|
|
|
for neighbor in maze.get_neighbors(current):
|
|
|
|
if neighbor in visited:
|
|
continue
|
|
|
|
visited.add(neighbor)
|
|
parents[neighbor] = current
|
|
queue.append(neighbor)
|
|
|
|
return [], visited_count |