diff --git a/skorohodovsa/task_2/source/models/base.py b/skorohodovsa/task_2/source/models/base.py index 632d777..52c874a 100644 --- a/skorohodovsa/task_2/source/models/base.py +++ b/skorohodovsa/task_2/source/models/base.py @@ -183,6 +183,22 @@ class Maze: neighbors.append(cell) return neighbors + + @property + def start(self) -> Optional[Cell]: + for y in range(self._height): + for x in range(self._width): + if self[y, x].is_start: + return self[y, x] + return None + + @property + def exit(self) -> Optional[Cell]: + for y in range(self._height): + for x in range(self._width): + if self[y, x].is_exit: + return self[y, x] + return None @property def shape(self) -> tuple[int, int]: diff --git a/skorohodovsa/task_2/source/strategy/algorithms.py b/skorohodovsa/task_2/source/strategy/algorithms.py index 0c17a8d..c6fd162 100644 --- a/skorohodovsa/task_2/source/strategy/algorithms.py +++ b/skorohodovsa/task_2/source/strategy/algorithms.py @@ -23,24 +23,6 @@ class PathFindingStrategy(ABC): Пустой список, если путь не найден. """ - def _find_start(self, maze: Maze) -> Optional[Cell]: - row, col = maze.shape - - for y in range(row): - for x in range(col): - if maze[y, x].is_start: - return maze[y, x] - return None - - def _find_exit(self, maze: Maze) -> Optional[Cell]: - row, col = maze.shape - - for y in range(row): - for x in range(col): - if maze[y, x].is_exit: - return maze[y, x] - return None - def _reconstruct_path( self, came_from: dict[Cell, Optional[Cell]], end: Cell ) -> list[Cell]: diff --git a/skorohodovsa/task_2/source/strategy/astar.py b/skorohodovsa/task_2/source/strategy/astar.py index ba07fce..37f76b0 100644 --- a/skorohodovsa/task_2/source/strategy/astar.py +++ b/skorohodovsa/task_2/source/strategy/astar.py @@ -15,9 +15,9 @@ class AStarStrategy(PathFindingStrategy): def find_path(self, maze: Maze, start: Optional[Cell] = None, exit: Optional[Cell] = None) -> list[Cell]: if start is None: - start = self._find_start(maze) + start = maze.start if exit is None: - exit = self._find_exit(maze) + exit = self.exit g_score: dict[Cell, int] = {start: 0} came_from: dict[Cell, Optional[Cell]] = {start: None} diff --git a/skorohodovsa/task_2/source/strategy/bfs.py b/skorohodovsa/task_2/source/strategy/bfs.py index 39e6595..fb6317f 100644 --- a/skorohodovsa/task_2/source/strategy/bfs.py +++ b/skorohodovsa/task_2/source/strategy/bfs.py @@ -16,9 +16,9 @@ class BFSStrategy(PathFindingStrategy): self, maze: Maze, start: Optional[Cell] = None, exit: Optional[Cell] = None ) -> list[Cell]: if start is None: - start = self._find_start(maze) + start = maze.start if exit is None: - exit = self._find_exit(maze) + exit = maze.exit came_from: dict[Cell, Optional[Cell]] = {start: None} queue: deque[Cell] = deque([start]) diff --git a/skorohodovsa/task_2/source/strategy/dfs.py b/skorohodovsa/task_2/source/strategy/dfs.py index c24a1e2..11cde79 100644 --- a/skorohodovsa/task_2/source/strategy/dfs.py +++ b/skorohodovsa/task_2/source/strategy/dfs.py @@ -14,9 +14,9 @@ class DFSStrategy(PathFindingStrategy): self, maze: Maze, start: Optional[Cell] = None, exit: Optional[Cell] = None ) -> list[Cell]: if start is None: - start = self._find_start(maze) + start = maze.start if exit is None: - exit = self._find_exit(maze) + exit = maze.exit came_from: dict[Cell, Optional[Cell]] = {start: None} stack: list[Cell] = [start]