[1] Лабораторная работа №1 "Структуры данных" #198
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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` - Конец лабиринта
|
||||
|
||||
` `(пробел) - свободный проход
|
||||
|
||||
`<int>` - Вес клетки
|
||||
"""
|
||||
if self.isWall:
|
||||
return '#'
|
||||
elif self.isStart:
|
||||
|
|
@ -35,4 +75,9 @@ class Cell:
|
|||
elif self.isExit:
|
||||
return 'E'
|
||||
else:
|
||||
return ' '
|
||||
return ' '
|
||||
|
||||
@property
|
||||
def value(self) -> int:
|
||||
"""Возвращает вес клетки"""
|
||||
return self.value
|
||||
|
|
@ -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='')
|
||||
|
|
|
|||
10
stepushovgs/labyrinth/source/strategy/A.py
Normal file
10
stepushovgs/labyrinth/source/strategy/A.py
Normal file
|
|
@ -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
|
||||
10
stepushovgs/labyrinth/source/strategy/BFS.py
Normal file
10
stepushovgs/labyrinth/source/strategy/BFS.py
Normal file
|
|
@ -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
|
||||
10
stepushovgs/labyrinth/source/strategy/DFS.py
Normal file
10
stepushovgs/labyrinth/source/strategy/DFS.py
Normal file
|
|
@ -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
|
||||
10
stepushovgs/labyrinth/source/strategy/Dijkstra.py
Normal file
10
stepushovgs/labyrinth/source/strategy/Dijkstra.py
Normal file
|
|
@ -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
|
||||
14
stepushovgs/labyrinth/source/strategy/strategy.py
Normal file
14
stepushovgs/labyrinth/source/strategy/strategy.py
Normal file
|
|
@ -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
|
||||
|
|
@ -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"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user