From d8eebad0a5a73a2f263cd6c46e762707292b8552 Mon Sep 17 00:00:00 2001 From: Veronika Minina Date: Sun, 17 May 2026 14:48:02 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=B0=D0=BD=D1=85=D1=8D=D1=82=D1=82?= =?UTF-8?q?=D0=B5=D0=BD=D1=81=D0=BA=D0=B0=D1=8F=20=D1=8D=D0=B2=D1=80=D0=B8?= =?UTF-8?q?=D1=81=D1=82=D0=B8=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data2/strategiesA_star_strategy.ipynb | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 MininaVD/docs2/data2/strategiesA_star_strategy.ipynb diff --git a/MininaVD/docs2/data2/strategiesA_star_strategy.ipynb b/MininaVD/docs2/data2/strategiesA_star_strategy.ipynb new file mode 100644 index 0000000..0f3b90b --- /dev/null +++ b/MininaVD/docs2/data2/strategiesA_star_strategy.ipynb @@ -0,0 +1,90 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "f8e6c2ad-712c-44a0-8ebc-ed0d67234c05", + "metadata": {}, + "outputs": [], + "source": [ + "import heapq\n", + "from typing import List, Dict, Optional, Tuple\n", + "from strategiesPathfinding_strategy import PathFindingStrategy\n", + "from modelsMaze import Maze\n", + "from modelsCell import Cell\n", + "\n", + "class AStarStrategy(PathFindingStrategy):\n", + " \"\"\"Алгоритм A* с манхэттенской эвристикой.\"\"\"\n", + " \n", + " @property\n", + " def name(self) -> str:\n", + " return \"A*\"\n", + " \n", + " def _heuristic(self, a: Cell, b: Cell) -> int:\n", + " \"\"\"Манхэттенское расстояние.\"\"\"\n", + " return abs(a.x - b.x) + abs(a.y - b.y)\n", + " \n", + " def find_path(self, maze: Maze, start: Cell, exit_cell: Cell) -> List[Cell]:\n", + " if start == exit_cell:\n", + " return [start]\n", + " \n", + " # Приоритетная очередь: (f_score, counter, cell)\n", + " open_set = [(0, 0, start)]\n", + " counter = 1\n", + " \n", + " came_from: Dict[Cell, Optional[Cell]] = {}\n", + " \n", + " g_score: Dict[Cell, float] = {start: 0}\n", + " f_score: Dict[Cell, float] = {start: self._heuristic(start, exit_cell)}\n", + " \n", + " visited_count = 0\n", + " \n", + " while open_set:\n", + " current_f, _, current = heapq.heappop(open_set)\n", + " visited_count += 1\n", + " \n", + " if current == exit_cell:\n", + " self._last_visited_count = visited_count\n", + " return self._reconstruct_path(came_from, start, current)\n", + " \n", + " for neighbor in maze.get_neighbors(current):\n", + " tentative_g_score = g_score.get(current, float('inf')) + 1\n", + " \n", + " if tentative_g_score < g_score.get(neighbor, float('inf')):\n", + " came_from[neighbor] = current\n", + " g_score[neighbor] = tentative_g_score\n", + " f_score[neighbor] = tentative_g_score + self._heuristic(neighbor, exit_cell)\n", + " heapq.heappush(open_set, (f_score[neighbor], counter, neighbor))\n", + " counter += 1\n", + " \n", + " self._last_visited_count = visited_count\n", + " return []\n", + " \n", + " @property\n", + " def last_visited_count(self) -> int:\n", + " return getattr(self, '_last_visited_count', 0)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:base] *", + "language": "python", + "name": "conda-base-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}