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