diff --git a/MusinAA/task2/strategyObjects/BFS.py b/MusinAA/task2/strategyObjects/BFS.py index becac59..d533fb8 100644 --- a/MusinAA/task2/strategyObjects/BFS.py +++ b/MusinAA/task2/strategyObjects/BFS.py @@ -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] \ No newline at end of file + return restorePath(parents, start, exit) \ No newline at end of file diff --git a/MusinAA/task2/strategyObjects/DFS.py b/MusinAA/task2/strategyObjects/DFS.py index 88f581c..7a05d4d 100644 --- a/MusinAA/task2/strategyObjects/DFS.py +++ b/MusinAA/task2/strategyObjects/DFS.py @@ -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] - \ No newline at end of file + return restorePath(parents, start, exit) \ No newline at end of file diff --git a/MusinAA/task2/strategyObjects/util.py b/MusinAA/task2/strategyObjects/util.py new file mode 100644 index 0000000..8fd3c4d --- /dev/null +++ b/MusinAA/task2/strategyObjects/util.py @@ -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] \ No newline at end of file