[1] Лабораторная работа №1 "Структуры данных" #198

Merged
kit8nino merged 18 commits from stepushovgs/2026-rff_mp:stepushovgs into develop 2026-05-30 11:41:24 +00:00
4 changed files with 125 additions and 12 deletions
Showing only changes of commit c8694aa053 - Show all commits

View File

@ -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
# 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

View File

@ -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()

View File

@ -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,