From 107d5cbd61abf07aedcde09d3246f583af3882ab Mon Sep 17 00:00:00 2001 From: oSTEVEo Date: Wed, 20 May 2026 21:23:47 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=20=D1=8D=D1=82=D0=B0=D0=BF=203,=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20A*,=20=D0=BD=D0=B5?= =?UTF-8?q?=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B=D0=B5=20=D0=B8=D1=81=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MusinAA/docs/Report 1.ipynb | 2 +- MusinAA/task2/strategyObjects/AStar.py | 38 ++++++++++++++++++- MusinAA/task2/strategyObjects/BFS.py | 2 +- MusinAA/task2/strategyObjects/DFS.py | 2 +- .../strategyObjects/pathFindingStrategy.py | 3 +- MusinAA/task2/strategyObjects/util.py | 2 +- 6 files changed, 43 insertions(+), 6 deletions(-) diff --git a/MusinAA/docs/Report 1.ipynb b/MusinAA/docs/Report 1.ipynb index 3af91fe..f476d56 100644 --- a/MusinAA/docs/Report 1.ipynb +++ b/MusinAA/docs/Report 1.ipynb @@ -140,7 +140,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.14.3" + "version": "3.14.5" } }, "nbformat": 4, diff --git a/MusinAA/task2/strategyObjects/AStar.py b/MusinAA/task2/strategyObjects/AStar.py index 7b1b45e..5b4dde1 100644 --- a/MusinAA/task2/strategyObjects/AStar.py +++ b/MusinAA/task2/strategyObjects/AStar.py @@ -1,9 +1,45 @@ +import heapq +from itertools import count + from task2.strategyObjects.pathFindingStrategy import PathFindingStrategy +from task2.strategyObjects.util import restorePath from task2.mazeObjects.maze import Maze from task2.mazeObjects.cell import Cell class AStar(PathFindingStrategy): """Алгоритм с эвристикой (etc. манхэттенское расстояние) – компромисс между скоростью и оптимальностью.""" + def heuristic(self, first: Cell, second: Cell) -> int: + return abs(first.x - second.x) + abs(first.y - second.y) + def findPath(self, maze: Maze, start: Cell, exit: Cell): - ... \ No newline at end of file + tie_breaker = count() + start_heuristic = self.heuristic(start, exit) + heap: list[tuple[int, int, int, Cell]] = [ + (start_heuristic, start_heuristic, next(tie_breaker), start) + ] + g_score: dict[Cell, int] = {start: 0} + parents: dict[Cell, Cell | None] = {start: None} + visited: set[Cell] = set() + + while heap: + _, _, _, current = heapq.heappop(heap) + if current in visited: + continue + visited.add(current) + + if current == exit: + return restorePath(parents, exit) + + for neighbor in maze.getNeighbors(current): + tentative_score = g_score[current] + if tentative_score < g_score.get(neighbor, 10**12): + g_score[neighbor] = tentative_score + parents[neighbor] = current + heuristic = self.heuristic(neighbor, exit) + priority = tentative_score + heuristic + heapq.heappush( + heap, + (priority, heuristic, next(tie_breaker), neighbor), + ) + return [] \ No newline at end of file diff --git a/MusinAA/task2/strategyObjects/BFS.py b/MusinAA/task2/strategyObjects/BFS.py index d533fb8..6379a6a 100644 --- a/MusinAA/task2/strategyObjects/BFS.py +++ b/MusinAA/task2/strategyObjects/BFS.py @@ -31,4 +31,4 @@ class BFS(PathFindingStrategy): visited[hood] = visited[current] + 1 parents[hood] = current q.put(hood) - return restorePath(parents, start, exit) \ No newline at end of file + return restorePath(parents, exit) \ No newline at end of file diff --git a/MusinAA/task2/strategyObjects/DFS.py b/MusinAA/task2/strategyObjects/DFS.py index 7a05d4d..b4e0fb6 100644 --- a/MusinAA/task2/strategyObjects/DFS.py +++ b/MusinAA/task2/strategyObjects/DFS.py @@ -31,4 +31,4 @@ class DFS(PathFindingStrategy): parents[hood] = current stack.append(hood) - return restorePath(parents, start, exit) \ No newline at end of file + return restorePath(parents, exit) \ No newline at end of file diff --git a/MusinAA/task2/strategyObjects/pathFindingStrategy.py b/MusinAA/task2/strategyObjects/pathFindingStrategy.py index 0a1cb92..800cb5b 100644 --- a/MusinAA/task2/strategyObjects/pathFindingStrategy.py +++ b/MusinAA/task2/strategyObjects/pathFindingStrategy.py @@ -9,4 +9,5 @@ class PathFindingStrategy(ABC): @abstractmethod def findPath(self, maze: Maze, start: Cell, exit: Cell): - """Возвращает список клеток пути от старта до выхода включительно. Пути нет - пустой список.""" \ No newline at end of file + """Возвращает список клеток пути от старта до выхода включительно. Пути нет - пустой список.""" + raise NotImplementedError \ No newline at end of file diff --git a/MusinAA/task2/strategyObjects/util.py b/MusinAA/task2/strategyObjects/util.py index 8fd3c4d..26dd777 100644 --- a/MusinAA/task2/strategyObjects/util.py +++ b/MusinAA/task2/strategyObjects/util.py @@ -1,7 +1,7 @@ from task2.mazeObjects.maze import Maze from task2.mazeObjects.cell import Cell -def restorePath(parents: dict, start: Cell, exit: Cell) -> list[Cell]|None: +def restorePath(parents: dict, exit: Cell) -> list[Cell]|None: path = [] current = exit while current: