From ccb1fca2a93e1e5dc4ca6fdfc39fd4ac438f4008 Mon Sep 17 00:00:00 2001 From: Veronika Minina Date: Sun, 17 May 2026 14:38:41 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=B8=D1=81=D0=BA=20=D0=B2=20?= =?UTF-8?q?=D0=B3=D0=BB=D1=83=D0=B1=D0=B8=D0=BD=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../docs2/data2/strategiesDfs_strategy.ipynb | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 MininaVD/docs2/data2/strategiesDfs_strategy.ipynb diff --git a/MininaVD/docs2/data2/strategiesDfs_strategy.ipynb b/MininaVD/docs2/data2/strategiesDfs_strategy.ipynb new file mode 100644 index 0000000..5ce0f5e --- /dev/null +++ b/MininaVD/docs2/data2/strategiesDfs_strategy.ipynb @@ -0,0 +1,73 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "4d8d9af1-63da-4f25-9140-a0b3c58cb96d", + "metadata": {}, + "outputs": [], + "source": [ + "from typing import List, Dict, Optional\n", + "from strategiesPathfinding_strategy import PathFindingStrategy\n", + "from modelsMaze import Maze\n", + "from modelsCell import Cell\n", + "\n", + "class DFSStrategy(PathFindingStrategy):\n", + " \"\"\"Поиск в глубину - быстрый, но не обязательно кратчайший.\"\"\"\n", + " \n", + " @property\n", + " def name(self) -> str:\n", + " return \"DFS\"\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", + " stack = [start]\n", + " came_from: Dict[Cell, Optional[Cell]] = {start: None}\n", + " visited_count = 0\n", + " \n", + " while stack:\n", + " current = stack.pop()\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", + " if neighbor not in came_from:\n", + " came_from[neighbor] = current\n", + " stack.append(neighbor)\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 +}