[2] 2-st-exercise #356
|
|
@ -61,11 +61,8 @@ class MazeBuilder:
|
|||
raise ValueError("Лабиринт сломан")
|
||||
return maze
|
||||
|
||||
|
||||
|
||||
class PathFindingStrategy:
|
||||
def findPath(self, maze, start, exit):
|
||||
|
||||
raise NotImplementedError("Этот метод должен быть реализован в стратегии!")
|
||||
|
||||
def _reconstruct_path(self, parents, current):
|
||||
|
|
@ -75,8 +72,6 @@ class PathFindingStrategy:
|
|||
current = parents.get(current)
|
||||
return path[::-1]
|
||||
|
||||
|
||||
|
||||
class BFSStrategy(PathFindingStrategy):
|
||||
def findPath(self, maze, start, exit):
|
||||
queue = deque([start])
|
||||
|
|
@ -126,4 +121,32 @@ class SearchStats:
|
|||
class MazeSolver:
|
||||
def __init__(self, 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