{ "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 }