From 86b31473f3a5d19c504ce37ee9a398e9d4226fbb Mon Sep 17 00:00:00 2001 From: SerKin0 <71343548+SerKin0@users.noreply.github.com> Date: Sun, 24 May 2026 17:58:56 +0300 Subject: [PATCH] =?UTF-8?q?[2]=20=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=BE=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B9=20`?= =?UTF-8?q?=5Ffind=5Fstart()`=20=D0=B8=20`=5Ffind=5Fexit()`=20=D0=B8=D0=B7?= =?UTF-8?q?=20`PathFindingStrategy`=20=D0=B2=20=D1=84=D1=83=D0=BD=D0=BA?= =?UTF-8?q?=D1=86=D0=B8=D0=BE=D0=BD=D0=B0=D0=BB=20=D0=BA=D0=BB=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D0=B0=20`Maze`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- skorohodovsa/task_2/source/models/base.py | 16 ++++++++++++++++ .../task_2/source/strategy/algorithms.py | 18 ------------------ skorohodovsa/task_2/source/strategy/astar.py | 4 ++-- skorohodovsa/task_2/source/strategy/bfs.py | 4 ++-- skorohodovsa/task_2/source/strategy/dfs.py | 4 ++-- 5 files changed, 22 insertions(+), 24 deletions(-) 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]