From 245a1e7c4b932526203bcb22610c6679a90d68c4 Mon Sep 17 00:00:00 2001 From: volkovva Date: Mon, 25 May 2026 03:30:40 +0300 Subject: [PATCH] =?UTF-8?q?=20=D1=81=D1=82=D1=80=D0=B0=D1=82=D0=B5=D0=B3?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=B0=20=D0=BF?= =?UTF-8?q?=D1=83=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VolkovVA/cod.py | 81 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 10 deletions(-) diff --git a/VolkovVA/cod.py b/VolkovVA/cod.py index 56204c1..fa15e82 100644 --- a/VolkovVA/cod.py +++ b/VolkovVA/cod.py @@ -1,3 +1,8 @@ +from abc import ABC, abstractmethod +from collections import deque +import heapq + +# --- Модель --- class Cell: def __init__(self, x, y, is_wall=False, is_start=False, is_exit=False): self.x = x @@ -5,6 +10,7 @@ class Cell: self.is_wall = is_wall self.is_start = is_start self.is_exit = is_exit + self.visited = False def is_passable(self): return not self.is_wall @@ -31,6 +37,7 @@ class Maze: neighbors.append(neighbor) return neighbors +# --- Строитель class MazeBuilder: def buildFromFile(self, filename): with open(filename, 'r') as f: @@ -38,32 +45,86 @@ class MazeBuilder: height = len(lines) width = len(lines[0].strip()) - maze = Maze(width, height) for y, line in enumerate(lines): for x, char in enumerate(line.strip()): cell = maze.get_cell(x, y) - if char == '#': - cell.is_wall = True + if char == '#': cell.is_wall = True elif char == 'S': cell.is_start = True maze.start_cell = cell elif char == 'E': cell.is_exit = True maze.exit_cell = cell - if not maze.start_cell or not maze.exit_cell: raise ValueError("Лабиринт сломан") - return maze + + +class PathFindingStrategy(ABC): + @abstractmethod + def findPath(self, maze, start, exit): + pass + + def _reconstruct_path(self, parents, current): + path = [] + while current: + path.append(current) + current = parents.get(current) + return path[::-1] + +class BFSStrategy(PathFindingStrategy): + def findPath(self, maze, start, exit): + queue = deque([start]) + parents = {start: None} + start.visited = True + + while queue: + current = queue.popleft() + if current == exit: + return self._reconstruct_path(parents, exit) + for neighbor in maze.get_neighbors(current): + if not neighbor.visited: + neighbor.visited = True + parents[neighbor] = current + queue.append(neighbor) + return [] + +class AStarStrategy(PathFindingStrategy): + def _heuristic(self, a, b): + return abs(a.x - b.x) + abs(a.y - b.y) + + def findPath(self, maze, start, exit): + heap = [(0, start)] + parents = {start: None} + g_score = {start: 0} + + while heap: + _, current = heapq.heappop(heap) + if current == exit: + return self._reconstruct_path(parents, exit) + + for neighbor in maze.get_neighbors(current): + new_g = g_score[current] + 1 + if new_g < g_score.get(neighbor, float('inf')): + parents[neighbor] = current + g_score[neighbor] = new_g + f = new_g + self._heuristic(neighbor, exit) + heapq.heappush(heap, (f, neighbor)) + return [] + if __name__ == "__main__": builder = MazeBuilder() try: + path_to_maze = r"C:\Users\vva26\2026-rff_mp\VolkovVA\docs\data\maze.txt" + maze = builder.buildFromFile(path_to_maze) + print(f"Лабиринт {maze.width}x{maze.height} загружен.") + + + solver = BFSStrategy() + path = solver.findPath(maze, maze.start_cell, maze.exit_cell) + print(f" {len(path)}") - my_maze = builder.buildFromFile(r"C:\Users\vva26\2026-rff_mp\VolkovVA\docs\data\maze.txt") - print(f"Лабиринт успешно загружен! Размеры: {my_maze.width}x{my_maze.height}") - print(f"Старт в: ({my_maze.start_cell.x}, {my_maze.start_cell.y})") - print(f"Финиш в: ({my_maze.exit_cell.x}, {my_maze.exit_cell.y})") except Exception as e: - print(f"Ошибка при загрузке: {e}") \ No newline at end of file + print(f"Ошибка: {e}") \ No newline at end of file