From 047c2baf6dfab3e7a6ebe7f74b4f57957f1078ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=BE=D1=80=D0=B8=D1=81=D0=BE=D0=B2=20=D0=9C=D0=B0?= =?UTF-8?q?=D1=82=D0=B2=D0=B5=D0=B9?= Date: Wed, 20 May 2026 23:41:33 +0300 Subject: [PATCH] =?UTF-8?q?[2]=20=D0=A1=D1=82=D1=80=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D0=B3=D0=B8=D0=B8=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BF=D1=83=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BorisovMI/lab_2/docs/data/maze.py | 111 ++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/BorisovMI/lab_2/docs/data/maze.py b/BorisovMI/lab_2/docs/data/maze.py index 9d91122..b3173a7 100644 --- a/BorisovMI/lab_2/docs/data/maze.py +++ b/BorisovMI/lab_2/docs/data/maze.py @@ -120,3 +120,114 @@ class TextileMazeBuilder(MazeBuilder): return maze +class PathFindingStrategy: + def findPath(self, maze, start, exit): + pass + + +class BFSStrategy(PathFindingStrategy): + def findPath(self, maze, start, exit): + if exit is None: + return [] + queue = deque([start]) + visited = {start} + parent = {start: None} + + while queue: + current = queue.popleft() + + if current == exit: + return self._reconstruct_path(parent, start, exit) + + for neighbor in maze.getNeighbors(current): + if neighbor not in visited: + visited.add(neighbor) + parent[neighbor] = current + queue.append(neighbor) + + return [] + + def _reconstruct_path(self, parent, start, exit): + path = [] + current = exit + while current is not None: + path.append(current) + current = parent[current] + path.reverse() + return path + + +class DFSStrategy(PathFindingStrategy): + def findPath(self, maze, start, exit): + if exit is None: + return [] + stack = [start] + visited = {start} + parent = {start: None} + + while stack: + current = stack.pop() + + if current == exit: + return self._reconstruct_path(parent, start, exit) + + for neighbor in maze.getNeighbors(current): + if neighbor not in visited: + visited.add(neighbor) + parent[neighbor] = current + stack.append(neighbor) + + return [] + + def _reconstruct_path(self, parent, start, exit): + path = [] + current = exit + while current is not None: + path.append(current) + current = parent[current] + path.reverse() + return path + + +class AStrategy(PathFindingStrategy): + def _heuristic(self, cell, exit): + if exit is None: + return 0 + return abs(cell.x - exit.x) + abs(cell.y - exit.y) + + def findPath(self, maze, start, exit): + if exit is None: + return [] + open_set = [] + heapq.heappush(open_set, (0, start)) + + came_from = {start: None} + g_score = {start: 0} + + while open_set: + current = heapq.heappop(open_set)[1] + + if current == exit: + return self._reconstruct_path(came_from, start, exit) + + for neighbor in maze.getNeighbors(current): + tentative_g = g_score[current] + 1 + + if neighbor not in g_score or tentative_g < g_score[neighbor]: + came_from[neighbor] = current + g_score[neighbor] = tentative_g + f_score = tentative_g + self._heuristic(neighbor, exit) + heapq.heappush(open_set, (f_score, neighbor)) + + return [] + + def _reconstruct_path(self, came_from, start, exit): + path = [] + current = exit + while current is not None: + path.append(current) + current = came_from[current] + path.reverse() + return path + +