бета-тест этапа MazeSolver
This commit is contained in:
parent
f3908409ce
commit
031b48653f
|
|
@ -61,11 +61,8 @@ class MazeBuilder:
|
||||||
raise ValueError("Лабиринт сломан")
|
raise ValueError("Лабиринт сломан")
|
||||||
return maze
|
return maze
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class PathFindingStrategy:
|
class PathFindingStrategy:
|
||||||
def findPath(self, maze, start, exit):
|
def findPath(self, maze, start, exit):
|
||||||
|
|
||||||
raise NotImplementedError("Этот метод должен быть реализован в стратегии!")
|
raise NotImplementedError("Этот метод должен быть реализован в стратегии!")
|
||||||
|
|
||||||
def _reconstruct_path(self, parents, current):
|
def _reconstruct_path(self, parents, current):
|
||||||
|
|
@ -75,8 +72,6 @@ class PathFindingStrategy:
|
||||||
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])
|
||||||
|
|
@ -126,4 +121,32 @@ class SearchStats:
|
||||||
class MazeSolver:
|
class MazeSolver:
|
||||||
def __init__(self, maze):
|
def __init__(self, maze):
|
||||||
self.maze = maze
|
self.maze = maze
|
||||||
self.strat = None
|
self.strat = None
|
||||||
|
|
||||||
|
def setStrategy(self, strategy):
|
||||||
|
self.strat = strategy
|
||||||
|
|
||||||
|
def solve(self):
|
||||||
|
if not self.strat:
|
||||||
|
return None
|
||||||
|
|
||||||
|
t0 = time.perf_counter()
|
||||||
|
path = self.strat.findPath(self.maze, self.maze.start_cell, self.maze.exit_cell)
|
||||||
|
t1 = time.perf_counter()
|
||||||
|
|
||||||
|
# Считаем посещенные ячейки
|
||||||
|
visited_count = sum(c.visited for row in self.maze.cells for c in row)
|
||||||
|
|
||||||
|
return SearchStats((t1 - t0) * 1000, visited_count, len(path))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
builder = MazeBuilder()
|
||||||
|
try:
|
||||||
|
path = r"C:\Users\vva26\2026-rff_mp\VolkovVA\docs\data\maze.txt"
|
||||||
|
maze = builder.buildFromFile(path)
|
||||||
|
solver = MazeSolver(maze)
|
||||||
|
solver.setStrategy(BFSStrategy())
|
||||||
|
stats = solver.solve()
|
||||||
|
print(f"BFS: {stats.time_ms:.2f}мс, посещено: {stats.visited}, путь: {stats.length}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Ошибка: {e}")
|
||||||
Loading…
Reference in New Issue
Block a user