From 550af0164c672058845eff9d9e9bb3313ce71c31 Mon Sep 17 00:00:00 2001 From: Proninvv Date: Sat, 16 May 2026 20:46:12 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=B0=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=81=D1=82=D1=80=D0=B0=D1=82=D0=B5=D0=B3=D0=B8?= =?UTF-8?q?=D1=8F=20=D1=88=D0=B8=D1=80=D0=B8=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProninVV/task-2-oop/BreadthFirstSearch.py | 31 ++++++++++++++++++++ ProninVV/task-2-oop/{labirint.py => Maze.py} | 2 +- ProninVV/task-2-oop/MazeBuilder.py | 9 ++---- ProninVV/task-2-oop/main.py | 6 ++++ ProninVV/task-2-oop/strategy.py | 12 ++++++++ 5 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 ProninVV/task-2-oop/BreadthFirstSearch.py rename ProninVV/task-2-oop/{labirint.py => Maze.py} (95%) create mode 100644 ProninVV/task-2-oop/strategy.py diff --git a/ProninVV/task-2-oop/BreadthFirstSearch.py b/ProninVV/task-2-oop/BreadthFirstSearch.py new file mode 100644 index 0000000..6837a21 --- /dev/null +++ b/ProninVV/task-2-oop/BreadthFirstSearch.py @@ -0,0 +1,31 @@ +from strategy import PathFindingStrategy +from Maze import Maze, Cell + + +class BFSStrategy(PathFindingStrategy): + def findPath(maze: Maze, start: Cell, exit: Cell): + + if not start or not exit: + return [] + # очерель: перывй вошел - первый вышел + queue = [start] + # будем хранить откуда в какую клетку пришли + parents = {start: None} + + 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 + + childs = maze.getNeighbors(u) + for child in childs: + if child not in parents: + parents[child] = u + queue.append(child) + return [] diff --git a/ProninVV/task-2-oop/labirint.py b/ProninVV/task-2-oop/Maze.py similarity index 95% rename from ProninVV/task-2-oop/labirint.py rename to ProninVV/task-2-oop/Maze.py index f5fd69c..6140d80 100644 --- a/ProninVV/task-2-oop/labirint.py +++ b/ProninVV/task-2-oop/Maze.py @@ -25,7 +25,7 @@ class Maze: self.exit = exit def getCell(self, x, y) -> Cell: - if 0 <= x <= self.width and 0 <= y <= self.height: + if (0 <= x < self.width) and (0 <= y < self.height): return self.__grid[y][x] return None diff --git a/ProninVV/task-2-oop/MazeBuilder.py b/ProninVV/task-2-oop/MazeBuilder.py index 7aa4195..cff52e9 100644 --- a/ProninVV/task-2-oop/MazeBuilder.py +++ b/ProninVV/task-2-oop/MazeBuilder.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from labirint import Maze, Cell +from Maze import Maze, Cell class MazeBuilder(ABC): @@ -33,14 +33,11 @@ class TextFileMazeBuilder(MazeBuilder): elif char == 'E': cell.isExit = True self._maze.exit = cell + self._validate() return self._maze - def validate(self): + def _validate(self): if self._maze.start is None: raise "в лабиринте нет старта" if self._maze.exit is None: raise "в лабиринте нет начала" - - -# labir = TextFileMazeBuilder().buildFromFile('text.txt') -# print(labir) diff --git a/ProninVV/task-2-oop/main.py b/ProninVV/task-2-oop/main.py index 8b13789..77589db 100644 --- a/ProninVV/task-2-oop/main.py +++ b/ProninVV/task-2-oop/main.py @@ -1 +1,7 @@ +from MazeBuilder import TextFileMazeBuilder +from BreadthFirstSearch import BFSStrategy + +maze1 = TextFileMazeBuilder().buildFromFile("text.txt") +pathh = BFSStrategy.findPath(maze1, maze1.start, maze1.exit) +print(pathh) diff --git a/ProninVV/task-2-oop/strategy.py b/ProninVV/task-2-oop/strategy.py new file mode 100644 index 0000000..ffb6613 --- /dev/null +++ b/ProninVV/task-2-oop/strategy.py @@ -0,0 +1,12 @@ +from abc import ABC, abstractmethod +from typing import List +from Maze import Maze, Cell + +# интерфейс стратегий + + +class PathFindingStrategy(ABC): + @abstractmethod + def findPath(maze: Maze, start, exit) -> List[Cell]: + """ возвращает список клеток пути (от старта до выхода включительно) или пустой список, если пути нет """ + pass