2026-rff_mp/ProninVV/task-2-oop/BreadthFirstSearch.py

33 lines
1.0 KiB
Python
Raw Normal View History

from strategy import PathFindingStrategy
from Maze import Maze, Cell
class BFSStrategy(PathFindingStrategy):
def findPath(self, maze: Maze, start: Cell, exit: Cell):
# очерель: перывй вошел - первый вышел
queue = [start]
# будем хранить откуда в какую клетку пришли
parents = {start: None}
if not start or not exit:
return [], 0
while (len(queue) != 0):
u = queue.pop(0)
if u == exit:
path = []
current = exit
while current is not None:
path.append(current)
current = parents[current]
path.reverse()
return path, len(parents)
childs = maze.getNeighbors(u)
for child in childs:
if child not in parents:
parents[child] = u
queue.append(child)
return [], len(parents)