From e5493a54397ffa3240e0ebb960bf8d2622bac4e5 Mon Sep 17 00:00:00 2001 From: GordStep Date: Wed, 20 May 2026 21:42:45 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=20=D0=B7=D0=B0=D0=B3=D0=BE=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=B4=D0=BB=D1=8F=20=D0=B0=D0=BB=D0=B3=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D1=82=D0=BC=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../labyrinth/source/classes/builder.py | 3 +- stepushovgs/labyrinth/source/classes/cell.py | 49 ++++++++++++++++++- stepushovgs/labyrinth/source/classes/maze.py | 3 ++ stepushovgs/labyrinth/source/strategy/A.py | 10 ++++ stepushovgs/labyrinth/source/strategy/BFS.py | 10 ++++ stepushovgs/labyrinth/source/strategy/DFS.py | 10 ++++ .../labyrinth/source/strategy/Dijkstra.py | 10 ++++ .../labyrinth/source/strategy/strategy.py | 14 ++++++ stepushovgs/labyrinth/test.ipynb | 18 +++---- 9 files changed, 113 insertions(+), 14 deletions(-) create mode 100644 stepushovgs/labyrinth/source/strategy/A.py create mode 100644 stepushovgs/labyrinth/source/strategy/BFS.py create mode 100644 stepushovgs/labyrinth/source/strategy/DFS.py create mode 100644 stepushovgs/labyrinth/source/strategy/Dijkstra.py create mode 100644 stepushovgs/labyrinth/source/strategy/strategy.py diff --git a/stepushovgs/labyrinth/source/classes/builder.py b/stepushovgs/labyrinth/source/classes/builder.py index dc39544..3757511 100644 --- a/stepushovgs/labyrinth/source/classes/builder.py +++ b/stepushovgs/labyrinth/source/classes/builder.py @@ -12,8 +12,7 @@ class MazeBuilder(ABC): class TextFileMazeBuilder(MazeBuilder): def buildFromFile(self, filename: str) -> Maze: - - + """Получает лабиринт из текстового файла""" with open(filename) as f: data = f.read().splitlines() x, y = 0, 0 diff --git a/stepushovgs/labyrinth/source/classes/cell.py b/stepushovgs/labyrinth/source/classes/cell.py index 80e8be0..e8a3ea1 100644 --- a/stepushovgs/labyrinth/source/classes/cell.py +++ b/stepushovgs/labyrinth/source/classes/cell.py @@ -1,11 +1,38 @@ class Cell: + """ + Клетка лабиринта - def __init__(self, x, y, isWall=False, isStart=False, isExit=False): + `x, y` - координаты клетки в лабиринте + + `isWall` - Является ли клетка стеной + + `isStart` - Является ли клетка стартом + + `isExit` - Является ли клетка выходом лабиринта + + `value` - Вес клетки + """ + + def __init__(self, x: int, y: int, isWall=False, isStart=False, isExit=False, value=1): + """ + Создание клетки лабиринта + + `x, y` - координаты клетки в лабиринте + + `isWall` - Является ли клетка стеной + + `isStart` - Является ли клетка стартом + + `isExit` - Является ли клетка выходом лабиринта + + `value` - Вес клетки + """ self.__x = x self.__y = y self.isWall = isWall self.isStart = isStart self.isExit = isExit + self.valur = value pass @property @@ -28,6 +55,19 @@ class Cell: return self.__x, self.__y def toStr(self): + """ + Возвращает строчкое представление клетки + + `#` - Стена + + `S` - Начало лабиринта + + `E` - Конец лабиринта + + ` `(пробел) - свободный проход + + `` - Вес клетки + """ if self.isWall: return '#' elif self.isStart: @@ -35,4 +75,9 @@ class Cell: elif self.isExit: return 'E' else: - return ' ' \ No newline at end of file + return ' ' + + @property + def value(self) -> int: + """Возвращает вес клетки""" + return self.value \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/classes/maze.py b/stepushovgs/labyrinth/source/classes/maze.py index eca8815..6e3277d 100644 --- a/stepushovgs/labyrinth/source/classes/maze.py +++ b/stepushovgs/labyrinth/source/classes/maze.py @@ -1,6 +1,7 @@ from source.classes.cell import Cell class Maze: + """Лабиринт""" def __init__(self, cells, width, height, start, exit): self.cells = cells self.width = width @@ -13,6 +14,7 @@ class Maze: return self.cells[x][y] def getNeighbors(self, cell) -> list[Cell]: + """Возвращает список соседних проходимых клеток (вверх, вниз, влево, вправо, если в пределах границ и не стена).""" neighbors = [] c_x, c_y = cell.getXY() @@ -32,6 +34,7 @@ class Maze: return neighbors def printer(self): + """Выводит в консоль лабиринт (отладочное)""" for line in self.cells: for c in line: print(c.toStr(), end='') diff --git a/stepushovgs/labyrinth/source/strategy/A.py b/stepushovgs/labyrinth/source/strategy/A.py new file mode 100644 index 0000000..7bc933d --- /dev/null +++ b/stepushovgs/labyrinth/source/strategy/A.py @@ -0,0 +1,10 @@ +from source.strategy.strategy import PathFindingStrategy +from source.classes.maze import Maze +from source.classes.cell import Cell + +class AStrategy(PathFindingStrategy): + def findPath(self, maze: Maze, start: Cell, exit: Cell): + pass + + def __A__(self, maze: Maze, start: Cell, exit: Cell): + pass \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/strategy/BFS.py b/stepushovgs/labyrinth/source/strategy/BFS.py new file mode 100644 index 0000000..b6eb0b7 --- /dev/null +++ b/stepushovgs/labyrinth/source/strategy/BFS.py @@ -0,0 +1,10 @@ +from source.strategy.strategy import PathFindingStrategy +from source.classes.maze import Maze +from source.classes.cell import Cell + +class BFS(PathFindingStrategy): + def findPath(self, maze: Maze, start: Cell, exit: Cell): + pass + + def __BSF__(self, maze: Maze, start: Cell, exit: Cell): + pass \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/strategy/DFS.py b/stepushovgs/labyrinth/source/strategy/DFS.py new file mode 100644 index 0000000..1e5f9be --- /dev/null +++ b/stepushovgs/labyrinth/source/strategy/DFS.py @@ -0,0 +1,10 @@ +from source.strategy.strategy import PathFindingStrategy +from source.classes.maze import Maze +from source.classes.cell import Cell + +class DFS(PathFindingStrategy): + def findPath(self, maze: Maze, start: Cell, exit: Cell): + pass + + def __dfs__(self, maze: Maze, start: Cell, exit: Cell): + pass \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/strategy/Dijkstra.py b/stepushovgs/labyrinth/source/strategy/Dijkstra.py new file mode 100644 index 0000000..ec47c51 --- /dev/null +++ b/stepushovgs/labyrinth/source/strategy/Dijkstra.py @@ -0,0 +1,10 @@ +from source.strategy.strategy import PathFindingStrategy +from source.classes.maze import Maze +from source.classes.cell import Cell + +class Dijkstra(PathFindingStrategy): + def findPath(self, maze: Maze, start: Cell, exit: Cell): + pass + + def __dijkstra__(self, maze: Maze, start: Cell, exit: Cell): + pass \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/strategy/strategy.py b/stepushovgs/labyrinth/source/strategy/strategy.py new file mode 100644 index 0000000..e6fa8ff --- /dev/null +++ b/stepushovgs/labyrinth/source/strategy/strategy.py @@ -0,0 +1,14 @@ +from abc import ABC, abstractmethod + + +from source.classes.cell import Cell +from source.classes.maze import Maze + + +class PathFindingStrategy(ABC): + """Интерфейс для семейства алгоритмов поиска пути от старта до выхода.""" + + @abstractmethod + def findPath(self, maze: Maze, start: Cell, exit: Cell): + """Возвращающим список клеток пути (от старта до выхода включительно) или пустой список, если пути нет.""" + pass \ No newline at end of file diff --git a/stepushovgs/labyrinth/test.ipynb b/stepushovgs/labyrinth/test.ipynb index 35aacb6..a58e333 100644 --- a/stepushovgs/labyrinth/test.ipynb +++ b/stepushovgs/labyrinth/test.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "4489fc7e", "metadata": {}, "outputs": [ @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "fde1eddb", "metadata": {}, "outputs": [ @@ -36,14 +36,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "S#####\n", - " # # #\n", - " # #\n", - "## #\n", - " # #\n", - "#### #\n", - "# #\n", - "#E####\n" + "S # ###\n", + "## # # E\n", + "# # #\n", + "### ## #\n", + "# #\n", + "########\n" ] } ],