forked from UNN/2026-rff_mp
Реализован BFS, созданы шаблоны для всех алгоритмов
This commit is contained in:
parent
f3978396eb
commit
89c11085ba
9
MusinAA/task2/strategyObjects/AStar.py
Normal file
9
MusinAA/task2/strategyObjects/AStar.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
from task2.strategyObjects.pathFindingStrategy import PathFindingStrategy
|
||||||
|
|
||||||
|
from task2.mazeObjects.maze import Maze
|
||||||
|
from task2.mazeObjects.cell import Cell
|
||||||
|
|
||||||
|
class AStar(PathFindingStrategy):
|
||||||
|
"""Алгоритм с эвристикой (etc. манхэттенское расстояние) – компромисс между скоростью и оптимальностью."""
|
||||||
|
def findPath(self, maze: Maze, start: Cell, exit: Cell):
|
||||||
|
...
|
||||||
42
MusinAA/task2/strategyObjects/BFS.py
Normal file
42
MusinAA/task2/strategyObjects/BFS.py
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
from task2.strategyObjects.pathFindingStrategy import PathFindingStrategy
|
||||||
|
|
||||||
|
from task2.mazeObjects.maze import Maze
|
||||||
|
from task2.mazeObjects.cell import Cell
|
||||||
|
|
||||||
|
import queue
|
||||||
|
|
||||||
|
class BFS(PathFindingStrategy):
|
||||||
|
"""Поиск в ширину – гарантирует кратчайший путь по количеству шагов."""
|
||||||
|
def findPath(self, maze: Maze, start: Cell, exit: Cell):
|
||||||
|
visited = dict()
|
||||||
|
parents = dict()
|
||||||
|
q = queue.Queue()
|
||||||
|
|
||||||
|
q.put(start)
|
||||||
|
visited[start] = 0
|
||||||
|
parents[start] = None
|
||||||
|
|
||||||
|
while not q.empty():
|
||||||
|
current = q.get()
|
||||||
|
|
||||||
|
# Условие нахождение выхода
|
||||||
|
if current == exit: break
|
||||||
|
|
||||||
|
# Перебор соседей
|
||||||
|
for hood in maze.getNeighbors(current):
|
||||||
|
if hood in visited:
|
||||||
|
continue
|
||||||
|
visited[hood] = visited[current] + 1
|
||||||
|
parents[hood] = current
|
||||||
|
q.put(hood)
|
||||||
|
return self.restorePath(parents, start, exit)
|
||||||
|
|
||||||
|
def restorePath(self, parents: dict, start: Cell, exit: Cell) -> list[Cell]|None:
|
||||||
|
path = []
|
||||||
|
current = exit
|
||||||
|
while current:
|
||||||
|
path.append(current)
|
||||||
|
if current not in parents:
|
||||||
|
return []
|
||||||
|
current = parents[current]
|
||||||
|
return path[::-1]
|
||||||
9
MusinAA/task2/strategyObjects/DFS.py
Normal file
9
MusinAA/task2/strategyObjects/DFS.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
from task2.strategyObjects.pathFindingStrategy import PathFindingStrategy
|
||||||
|
|
||||||
|
from task2.mazeObjects.maze import Maze
|
||||||
|
from task2.mazeObjects.cell import Cell
|
||||||
|
|
||||||
|
class DFS(PathFindingStrategy):
|
||||||
|
"""Поиск в глубину – быстрый, но не обязательно кратчайший."""
|
||||||
|
def findPath(self, maze: Maze, start: Cell, exit: Cell):
|
||||||
|
...
|
||||||
0
MusinAA/task2/strategyObjects/__init__.py
Normal file
0
MusinAA/task2/strategyObjects/__init__.py
Normal file
12
MusinAA/task2/strategyObjects/pathFindingStrategy.py
Normal file
12
MusinAA/task2/strategyObjects/pathFindingStrategy.py
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
from task2.mazeObjects.maze import Maze
|
||||||
|
from task2.mazeObjects.cell import Cell
|
||||||
|
|
||||||
|
class PathFindingStrategy(ABC):
|
||||||
|
"""Интерфейс PathFindingStrategy с методом findPath(maze, start, exit),
|
||||||
|
возвращающим список клеток пути (от старта до выхода включительно) или пустой список, если пути нет."""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def findPath(self, maze: Maze, start: Cell, exit: Cell):
|
||||||
|
"""Возвращает список клеток пути от старта до выхода включительно. Пути нет - пустой список."""
|
||||||
Loading…
Reference in New Issue
Block a user