написаны MazeSolver, SearchStats

This commit is contained in:
volkovva 2026-05-25 03:52:35 +03:00
parent 245a1e7c4b
commit f3908409ce

View File

@ -1,6 +1,6 @@
from abc import ABC, abstractmethod
from collections import deque
import heapq
import time
# --- Модель ---
class Cell:
@ -62,10 +62,11 @@ class MazeBuilder:
return maze
class PathFindingStrategy(ABC):
@abstractmethod
class PathFindingStrategy:
def findPath(self, maze, start, exit):
pass
raise NotImplementedError("Этот метод должен быть реализован в стратегии!")
def _reconstruct_path(self, parents, current):
path = []
@ -74,6 +75,8 @@ class PathFindingStrategy(ABC):
current = parents.get(current)
return path[::-1]
class BFSStrategy(PathFindingStrategy):
def findPath(self, maze, start, exit):
queue = deque([start])
@ -114,17 +117,13 @@ class AStarStrategy(PathFindingStrategy):
heapq.heappush(heap, (f, neighbor))
return []
if __name__ == "__main__":
builder = MazeBuilder()
try:
path_to_maze = r"C:\Users\vva26\2026-rff_mp\VolkovVA\docs\data\maze.txt"
maze = builder.buildFromFile(path_to_maze)
print(f"Лабиринт {maze.width}x{maze.height} загружен.")
solver = BFSStrategy()
path = solver.findPath(maze, maze.start_cell, maze.exit_cell)
print(f" {len(path)}")
except Exception as e:
print(f"Ошибка: {e}")
class SearchStats:
def __init__(self, time_ms, visited, length):
self.time_ms = time_ms
self.visited = visited
self.length = length
class MazeSolver:
def __init__(self, maze):
self.maze = maze
self.strat = None