Рефакторинг: Вынес restorePath() в отдельный общий файл

This commit is contained in:
oSTEVEo 2026-05-17 00:48:23 +03:00
parent 1a562a7594
commit d5f28df86a
3 changed files with 17 additions and 23 deletions

View File

@ -1,4 +1,5 @@
from task2.strategyObjects.pathFindingStrategy import PathFindingStrategy
from task2.strategyObjects.util import restorePath
from task2.mazeObjects.maze import Maze
from task2.mazeObjects.cell import Cell
@ -30,14 +31,4 @@ class BFS(PathFindingStrategy):
visited[hood] = visited[current] + 1
parents[hood] = current
q.put(hood)
return self.restorePath(parents, start, exit)
def restorePath(self, parents: dict, start: Cell, exit: Cell) -> list[Cell]|None:
path = []
current = exit
while current:
path.append(current)
if current not in parents:
return None
current = parents[current]
return path[::-1]
return restorePath(parents, start, exit)

View File

@ -1,8 +1,10 @@
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 DFS(PathFindingStrategy):
"""Поиск в глубину быстрый, но не обязательно кратчайший.
Возвращает None, если пути нет"""
@ -29,15 +31,4 @@ class DFS(PathFindingStrategy):
parents[hood] = current
stack.append(hood)
return self.restorePath(parents, start, exit)
def restorePath(self, parents: dict, start: Cell, exit: Cell) -> list[Cell]|None:
path = []
current = exit
while current:
path.append(current)
if current not in parents:
return None
current = parents[current]
return path[::-1]
return restorePath(parents, start, exit)

View File

@ -0,0 +1,12 @@
from task2.mazeObjects.maze import Maze
from task2.mazeObjects.cell import Cell
def restorePath(parents: dict, start: Cell, exit: Cell) -> list[Cell]|None:
path = []
current = exit
while current:
path.append(current)
if current not in parents:
return None
current = parents[current]
return path[::-1]