62 lines
1.3 KiB
Python
62 lines
1.3 KiB
Python
import time
|
|
|
|
class SearchStats:
|
|
def __init__(
|
|
self,
|
|
time_ms,
|
|
visited_cells,
|
|
path_length
|
|
):
|
|
self.time_ms = time_ms
|
|
self.visited_cells = visited_cells
|
|
self.path_length = path_length
|
|
def __str__(self):
|
|
return (
|
|
f"Время: "
|
|
f"{self.time_ms:.3f} мс\n"
|
|
|
|
f"Посещено клеток: "
|
|
f"{self.visited_cells}\n"
|
|
|
|
f"Длина пути: "
|
|
f"{self.path_length}"
|
|
)
|
|
|
|
class MazeSolver:
|
|
def __init__(
|
|
self,
|
|
maze,
|
|
strategy
|
|
):
|
|
self.maze = maze
|
|
self.strategy = strategy
|
|
def setStrategy(
|
|
self,
|
|
strategy
|
|
):
|
|
self.strategy = strategy
|
|
def solve(self):
|
|
start_time = (
|
|
time.perf_counter()
|
|
)
|
|
path, visited = (
|
|
self.strategy.findPath(
|
|
self.maze,
|
|
self.maze.start,
|
|
self.maze.exit
|
|
)
|
|
)
|
|
end_time = (
|
|
time.perf_counter()
|
|
)
|
|
time_ms = (
|
|
(end_time-start_time)
|
|
*1000
|
|
)
|
|
visited = len(path)
|
|
stats = SearchStats(
|
|
time_ms,
|
|
visited,
|
|
len(path)
|
|
)
|
|
return stats |