[2] MazeSolver
This commit is contained in:
parent
047c2baf6d
commit
098ccd7af1
|
|
@ -231,3 +231,40 @@ class AStrategy(PathFindingStrategy):
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
class SearchStats:
|
||||||
|
def __init__(self, time_ms=0, visited_cells=0, path_length=0):
|
||||||
|
self.time_ms = time_ms
|
||||||
|
self.visited_cells = visited_cells
|
||||||
|
self.path_length = path_length
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"Время: {self.time_ms:.3f} мс | Посещено: {self.visited_cells} | Длина пути: {self.path_length}"
|
||||||
|
|
||||||
|
|
||||||
|
class MazeSolver:
|
||||||
|
def __init__(self, maze):
|
||||||
|
self.maze = maze
|
||||||
|
self.strategy = None
|
||||||
|
|
||||||
|
def setStrategy(self, strategy):
|
||||||
|
self.strategy = strategy
|
||||||
|
|
||||||
|
def solve(self):
|
||||||
|
if self.strategy is None:
|
||||||
|
raise ValueError("Стратегия не установлена")
|
||||||
|
|
||||||
|
|
||||||
|
start_time = time.perf_counter()
|
||||||
|
path = self.strategy.findPath(self.maze, self.maze.start, self.maze.exit)
|
||||||
|
|
||||||
|
end_time = time.perf_counter()
|
||||||
|
elapsed_ms = (end_time - start_time) * 1000
|
||||||
|
|
||||||
|
|
||||||
|
stats = SearchStats(
|
||||||
|
time_ms=elapsed_ms,
|
||||||
|
visited_cells=len(path),
|
||||||
|
path_length=len(path)
|
||||||
|
)
|
||||||
|
|
||||||
|
return path, stats
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user