2026-rff_mp/ProninVV/task-2-oop/MazeSolver.py

43 lines
1.3 KiB
Python
Raw Normal View History

import time
class SearchStats:
def __init__(self, execution_time, visited_count, path_length, path):
self.execution_time = execution_time
self.visited_count = visited_count
self.path_length = path_length
self.path = path
def __str__(self):
return ("f == Статистика поиска == =\n"
f"Время выполнения: {self.execution_time_ms:.4f} мс\n"
f"Посещено клеток: {self.visited_count}\n"
f"Длина пути: {self.path_length} клеток\n")
class MazeSolver:
def __init__(self, maze, strategy):
self._maze = maze
self._strategy = strategy
def setStrategy(self, strategy):
self._strategy = strategy
def solve(self):
if not self.maze or not self.strategy:
raise ValueError("Не задан лабиринт или стратегия поиска!")
start_time = time.perf_counter()
path, visited_count = self.strategy.findPath(
self.maze, self.maze.start, self.maze.exit)
end_time = time.perf_counter()
execution_time_ms = (end_time - start_time) * 1000
path_length = len(path)
return SearchStats(execution_time_ms, visited_count, path_length, path)