Реализован BFS, созданы шаблоны для всех алгоритмов

This commit is contained in:
oSTEVEo 2026-05-16 19:20:59 +03:00
parent f3978396eb
commit 89c11085ba
5 changed files with 72 additions and 0 deletions

View 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):
...

View 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]

View 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):
...

View 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):
"""Возвращает список клеток пути от старта до выхода включительно. Пути нет - пустой список."""