From c8694aa0535694a6774011a6dea709fdaec04503 Mon Sep 17 00:00:00 2001 From: GordStep Date: Thu, 21 May 2026 23:40:09 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20BF?= =?UTF-8?q?S?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Реализовал bfs - Мелкие правки в dfs - Переместил command в command(хватит bububu) может потом допишу реализацию --- .../source/{bububu => command}/command.py | 0 stepushovgs/labyrinth/source/strategy/BFS.py | 48 ++++++++++-- stepushovgs/labyrinth/source/strategy/DFS.py | 13 ++-- stepushovgs/labyrinth/test.ipynb | 76 +++++++++++++++++++ 4 files changed, 125 insertions(+), 12 deletions(-) rename stepushovgs/labyrinth/source/{bububu => command}/command.py (100%) diff --git a/stepushovgs/labyrinth/source/bububu/command.py b/stepushovgs/labyrinth/source/command/command.py similarity index 100% rename from stepushovgs/labyrinth/source/bububu/command.py rename to stepushovgs/labyrinth/source/command/command.py diff --git a/stepushovgs/labyrinth/source/strategy/BFS.py b/stepushovgs/labyrinth/source/strategy/BFS.py index 998f1bd..ffa2c7a 100644 --- a/stepushovgs/labyrinth/source/strategy/BFS.py +++ b/stepushovgs/labyrinth/source/strategy/BFS.py @@ -1,13 +1,49 @@ -from source.strategy.strategy import PathFindingStrategy +from collections import deque + + +from source.strategy.strategy import PathFindingStrategy, reconstruct_path from source.classes.maze import Maze from source.classes.cell import Cell class BFS(PathFindingStrategy): - def findPath(self, maze: Maze): - pass - def name(self): + """Возвращает название метода""" return "BFS" + + def findPath(self, maze: Maze) -> tuple[list[Cell], int]: + start_cell = maze.start + exit_cell = maze.exit - def __BSF__(self, maze: Maze): - pass \ No newline at end of file + # print(f"Старт: {start_cell.getXY()}") + # print(f"Выход: {exit_cell.getXY()}") + # print(f"Соседи старта: {[n.getXY() for n in maze.getNeighbors(start_cell)]}") + + queue = deque([start_cell]) + + parents = {start_cell.getXY(): Cell(-1, -1)} + visited = {start_cell.getXY()} + count_visited = 1 + + while queue: + current = queue.popleft() + + if current.getXY() == exit_cell.getXY(): + return reconstruct_path( + came_from=parents, + start=start_cell, + end=current + ), count_visited + + # neigbours = maze.getNeighbors(current) + # print(f"для клекти {current.getXY()} соседи: {[neigbour.getXY() for neigbour in neigbours]}") + + for neighbor in maze.getNeighbors(current): + neig_xy = neighbor.getXY() + + if neig_xy not in visited: + visited.add(neig_xy) + parents[neig_xy] = current + count_visited += 1 + queue.append(neighbor) + + return [], count_visited \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/strategy/DFS.py b/stepushovgs/labyrinth/source/strategy/DFS.py index b195ca8..91a0587 100644 --- a/stepushovgs/labyrinth/source/strategy/DFS.py +++ b/stepushovgs/labyrinth/source/strategy/DFS.py @@ -5,21 +5,22 @@ from source.classes.cell import Cell class DFS(PathFindingStrategy): @property def name(self) -> str: + """Возвращает название метода""" return "DFS" def findPath(self, maze: Maze) -> tuple[list[Cell], int]: start_cell = maze.start exit_cell = maze.exit - print(f"Старт: {start_cell.getXY()}") - print(f"Выход: {exit_cell.getXY()}") - print(f"Соседи старта: {[n.getXY() for n in maze.getNeighbors(start_cell)]}") + # print(f"Старт: {start_cell.getXY()}") + # print(f"Выход: {exit_cell.getXY()}") + # print(f"Соседи старта: {[n.getXY() for n in maze.getNeighbors(start_cell)]}") stack = [start_cell] parents = {start_cell.getXY(): Cell(-1, -1)} visited = {start_cell.getXY()} - count_visited = 0 + count_visited = 1 while stack: current = stack.pop() @@ -31,8 +32,8 @@ class DFS(PathFindingStrategy): end=current ), count_visited - neigbours = maze.getNeighbors(current) - print(f"для клекти {current.getXY()} соседи: {[neigbour.getXY() for neigbour in neigbours]}") + # neigbours = maze.getNeighbors(current) + # print(f"для клекти {current.getXY()} соседи: {[neigbour.getXY() for neigbour in neigbours]}") for neighbor in maze.getNeighbors(current): neig_xy = neighbor.getXY() diff --git a/stepushovgs/labyrinth/test.ipynb b/stepushovgs/labyrinth/test.ipynb index 3f068c4..00b1ef5 100644 --- a/stepushovgs/labyrinth/test.ipynb +++ b/stepushovgs/labyrinth/test.ipynb @@ -164,6 +164,82 @@ "[cord.getXY() for cord in maze.getNeighbors(cell=Cell(2, 0))], [cord.getXY() for cord in stats.path]" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "73ba37a8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Старт: (0, 0)\n", + "Выход: (7, 1)\n", + "Соседи старта: [(1, 0)]\n", + "для клекти (0, 0) соседи: [(1, 0)]\n", + "для клекти (1, 0) соседи: [(0, 0), (2, 0)]\n", + "для клекти (2, 0) соседи: [(2, 1), (1, 0)]\n", + "для клекти (2, 1) соседи: [(2, 0), (2, 2)]\n", + "для клекти (2, 2) соседи: [(2, 1), (1, 2), (3, 2)]\n", + "для клекти (3, 2) соседи: [(3, 3), (2, 2), (4, 2)]\n", + "для клекти (4, 2) соседи: [(4, 1), (3, 2)]\n", + "для клекти (4, 1) соседи: [(4, 0), (4, 2)]\n", + "для клекти (4, 0) соседи: [(4, 1)]\n", + "для клекти (3, 3) соседи: [(3, 2), (3, 4)]\n", + "для клекти (3, 4) соседи: [(3, 3), (2, 4), (4, 4)]\n", + "для клекти (4, 4) соседи: [(3, 4), (5, 4)]\n", + "для клекти (5, 4) соседи: [(4, 4), (6, 4)]\n", + "для клекти (6, 4) соседи: [(6, 3), (5, 4)]\n", + "для клекти (6, 3) соседи: [(6, 2), (6, 4)]\n", + "для клекти (6, 2) соседи: [(6, 1), (6, 3)]\n", + "для клекти (6, 1) соседи: [(6, 2), (7, 1)]\n", + "Путь найден:\n", + "S**# ###\n", + "##*# #*E\n", + "# ** #*#\n", + "###*##*#\n", + "# ****#\n", + "########\n" + ] + }, + { + "data": { + "text/plain": [ + "([(2, 1), (1, 0)],\n", + " [(0, 0),\n", + " (1, 0),\n", + " (2, 0),\n", + " (2, 1),\n", + " (2, 2),\n", + " (3, 2),\n", + " (3, 3),\n", + " (3, 4),\n", + " (4, 4),\n", + " (5, 4),\n", + " (6, 4),\n", + " (6, 3),\n", + " (6, 2),\n", + " (6, 1),\n", + " (7, 1)])" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "from source.strategy.BFS import BFS\n", + "from source.strategy.maze_solver import MazeSolver\n", + "from source.classes.cell import Cell\n", + "\n", + "\n", + "solver = MazeSolver(maze, BFS(), ConsoleView())\n", + "stats = solver.solve()\n", + "\n", + "[cord.getXY() for cord in maze.getNeighbors(cell=Cell(2, 0))], [cord.getXY() for cord in stats.path]" + ] + }, { "cell_type": "code", "execution_count": 5,