From a27f861edff3a96a9dd6b153863a83419be52f3c Mon Sep 17 00:00:00 2001 From: Proninvv Date: Tue, 19 May 2026 20:44:08 +0300 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=20A*?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProninVV/task-2-oop/AStarStrategy.py | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 ProninVV/task-2-oop/AStarStrategy.py diff --git a/ProninVV/task-2-oop/AStarStrategy.py b/ProninVV/task-2-oop/AStarStrategy.py new file mode 100644 index 0000000..e366706 --- /dev/null +++ b/ProninVV/task-2-oop/AStarStrategy.py @@ -0,0 +1,41 @@ +from Maze import Cell, Maze +from strategy import PathFindingStrategy + + +class AStarStrategy(PathFindingStrategy): + def findPath(maze, start, exit): + + def heuristik(cell): + return abs(cell.x - exit.x) + abs(cell.y - exit.y) + + if not start or not exit: + return [] + + parents = {start: None} + + queue = [start] + + while len(queue) != 0: + best_cell = queue[0] + for cell in queue: + if heuristik(cell) < heuristik(best_cell): + best_cell = cell + + u = best_cell + queue.remove(u) + + if u == exit: + path = [] + current = exit + while current is not None: + path.append(current) + current = parents[current] + path.reverse() + return path + + childs = maze.getNeighbors(u) + for child in childs: + if child not in parents: + parents[child] = u + queue.append(child) + return []