From 64978d32451bb716086a42450e6c75711a33becc Mon Sep 17 00:00:00 2001 From: konnovaea Date: Mon, 18 May 2026 20:23:06 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D1=82=D1=80=D0=B0=D1=82=D0=B5=D0=B3?= =?UTF-8?q?=D0=B8=D0=B8=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D1=8E=D1=82?= =?UTF-8?q?=20=D1=82=D0=B5=D1=81=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BF=D1=80=D0=BE=D1=88=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- konnovaea/maze_solver.py | 136 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 129 insertions(+), 7 deletions(-) diff --git a/konnovaea/maze_solver.py b/konnovaea/maze_solver.py index 1adae7a..8b06667 100644 --- a/konnovaea/maze_solver.py +++ b/konnovaea/maze_solver.py @@ -106,15 +106,137 @@ class TextFileMazeBuilder(MazeBuilder): return maze - #тест +from collections import deque +import heapq +from abc import ABC, abstractmethod + +class PathfindingStrategy(ABC): + + @abstractmethod + def find_path(self, maze, start, exit): + pass + +class BFSStrategy(PathfindingStrategy): + + def find_path(self, maze, start, exit): + if start is None or exit is None: + return [] + + queue = deque() + queue.append((start, [start])) + + visited = set() + visited.add(start) + + while queue: + current, path = queue.popleft() + + if current == exit: + return path + + neighbors = maze.get_neighbors(current) + for neighbor in neighbors: + if neighbor not in visited: + visited.add(neighbor) + queue.append((neighbor, path + [neighbor])) + + return [] + +class DFSStrategy(PathfindingStrategy): + + def find_path(self, maze, start, exit): + if start is None or exit is None: + return [] + + stack = [(start, [start])] + + visited = set() + visited.add(start) + + while stack: + current, path = stack.pop() + + if current == exit: + return path + + for neighbor in maze.get_neighbors(current): + if neighbor not in visited: + visited.add(neighbor) + stack.append((neighbor, path + [neighbor])) + + return [] + +class AStartStrategy(PathfindingStrategy): + + def _heuristic(self, cell, exit): + return abs(cell.x - exit.x) + abs(cell.y -exit.y) + + def find_path(self, maze, start, exit): + if start is None or exit is None: + return [] + + counter = 0 + heap = [] + heapq.heappush(heap, (self._heuristic(start, exit), counter, start, [start])) + + g_score = {start: 0} + + visited = set() + + while heap: + f_score, _, current, path = heapq.heappop(heap) + + if current in visited: + continue + + visited.add(current) + + if current == exit: + return path + + for neighbor in maze.get_neighbors(current): + tentative_g = g_score[current] + 1 + + if neighbor not in g_score or tentative_g < g_score[neighbor]: + g_score[neighbor] = tentative_g + f_score = tentative_g + self._heuristic(neighbor, exit) + counter += 1 + heapq.heappush(heap, (f_score, counter, neighbor, path + [neighbor])) + + return [] + +#тест if __name__ == "__main__": builder = TextFileMazeBuilder() maze = builder.build_from_file("maze1.txt") - - print(f"лабиринт загружен: {maze}") - print(f"старт: {maze.start}") - print(f"выход: {maze.exit}") - + + print("Лабиринт загружен") + print(f"Старт: {maze.start}") + print(f"Выход: {maze.exit}") + + # Проверяем, что старт и выход проходимые + print(f"Старт проходим: {maze.start.is_passable()}") + print(f"Выход проходим: {maze.exit.is_passable()}") + + # Проверяем соседей старта neighbors = maze.get_neighbors(maze.start) - print(f"соседи старта: {neighbors}") \ No newline at end of file + print(f"Соседи старта: {neighbors}") + + # Тестируем BFS + bfs = BFSStrategy() + path = bfs.find_path(maze, maze.start, maze.exit) + print(f"BFS путь: {[f'({c.x},{c.y})' for c in path]}") + print(f"BFS длина пути: {len(path)}") + + # Тестируем DFS + dfs = DFSStrategy() + path = dfs.find_path(maze, maze.start, maze.exit) + print(f"DFS путь: {[f'({c.x},{c.y})' for c in path]}") + print(f"DFS длина пути: {len(path)}") + + # Тестируем A* + astar = AStartStrategy() + path = astar.find_path(maze, maze.start, maze.exit) + print(f"A* путь: {[f'({c.x},{c.y})' for c in path]}") + print(f"A* длина пути: {len(path)}") \ No newline at end of file