2026-rff_mp/stepushovgs/labyrinth/source/strategy/strategy.py

41 lines
1.5 KiB
Python

from abc import ABC, abstractmethod
from source.classes import Cell, Maze
class PathFindingStrategy(ABC):
"""Интерфейс для семейства алгоритмов поиска пути от старта до выхода."""
@abstractmethod
def findPath(self, maze: Maze) -> tuple[list[tuple[int, int]], int]:
"""Возвращающим список координат клеток пути (от старта до выхода включительно) или пустой список, если пути нет и количество посещённых клеток."""
pass
@property
@abstractmethod
def name(self) -> str:
"""Возвращает название алгоритма"""
pass
class CellAlgorithm(Cell):
def __init__(self, x: int, y: int, parent: Cell, exitDist: float, isWall=False, isStart=False, isExit=False, value=1):
super().__init__(x, y, isWall, isStart, isExit, value)
self.parent = parent
self.ExitDist = exitDist
self.weight = self.value + exitDist
def reconstruct_path(came_from: dict, start: Cell, end: Cell) -> list[Cell]:
"""Восстановление пути по словарю предшественников"""
path = []
current = end
# Идём от конца к началу по цепочке came_from
while current.getXY() != start.getXY():
path.append(current)
current = came_from[current.getXY()]
path.append(start)
return path[::-1]