2026-rff_mp/skorohodovsa/task_2/source/algorithms/strategy.py
SerKin0 535c706af8 [2] Добавление docstring к Cell, Maze
Добавлен интерфейс для алгоритмов
2026-05-23 16:30:04 +03:00

49 lines
1.8 KiB
Python

from abc import ABC, abstractmethod
from collections import deque
from typing import Optional
from source.models.base import Maze, Cell
class PathFindingStrategy(ABC):
"""Интерфейс стратегии поиска пути в лабиринте."""
@abstractmethod
def find_path(self, maze: Maze, start: Cell, exit: Cell) -> list[Cell]:
"""Найти путь от start до exit.
Args:
maze: Объект лабиринта.
start: Стартовая клетка.
exit: Целевая клетка.
Returns:
Список клеток пути (от start до exit включительно).
Пустой список, если путь не найден.
"""
def _reconstruct_path(came_from: dict[Cell, Optional[Cell]], end: Cell) -> list[Cell]:
"""Восстанавливает путь от старта до end, идя по came_from в обратном порядке.
Args:
came_from: Словарь {клетка: родитель}, где родитель старта = None.
end: Конечная клетка.
Returns:
Список клеток пути от старта до end включительно.
Пустой список, если end отсутствует в came_from.
"""
if end not in came_from:
return []
path: list[Cell] = []
current: Optional[Cell] = end
while current is not None:
path.append(current)
current = came_from[current]
path.reverse()
return path
class BFS(PathFindingStrategy):
def find_path(self, maze: Maze, start: Cell, exit: Cell) -> list[Cell]:
pass