forked from UNN/2026-rff_mp
Рефакторинг: Вынес restorePath() в отдельный общий файл
This commit is contained in:
parent
1a562a7594
commit
d5f28df86a
|
|
@ -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)
|
||||
|
|
@ -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)
|
||||
12
MusinAA/task2/strategyObjects/util.py
Normal file
12
MusinAA/task2/strategyObjects/util.py
Normal 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]
|
||||
Loading…
Reference in New Issue
Block a user