forked from UNN/2026-rff_mp
реализован поиск в глубину
This commit is contained in:
parent
550af0164c
commit
2449eeb26f
33
ProninVV/task-2-oop/DepthFirstSearch.py
Normal file
33
ProninVV/task-2-oop/DepthFirstSearch.py
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
from strategy import PathFindingStrategy
|
||||||
|
from Maze import Maze, Cell
|
||||||
|
|
||||||
|
|
||||||
|
class DFSStrategy(PathFindingStrategy):
|
||||||
|
def findPath(maze: Maze, start, exit):
|
||||||
|
|
||||||
|
if not start or not exit:
|
||||||
|
return []
|
||||||
|
|
||||||
|
visited = set()
|
||||||
|
path = []
|
||||||
|
|
||||||
|
def dfs(root: Cell) -> bool:
|
||||||
|
visited.add(root)
|
||||||
|
path.append(root)
|
||||||
|
|
||||||
|
if root == exit:
|
||||||
|
return True
|
||||||
|
|
||||||
|
neighbors = maze.getNeighbors(root)
|
||||||
|
for neighbor in neighbors:
|
||||||
|
if neighbor not in visited:
|
||||||
|
if dfs(neighbor):
|
||||||
|
return True
|
||||||
|
|
||||||
|
path.pop()
|
||||||
|
return False
|
||||||
|
|
||||||
|
if dfs(start):
|
||||||
|
return path
|
||||||
|
|
||||||
|
return []
|
||||||
Loading…
Reference in New Issue
Block a user