2026-rff_mp/pomelovsd/ExitMaze/Strategies/DFS.py
2026-05-25 16:02:23 +03:00

25 lines
652 B
Python

from Strategies.strat import PathFindingStrategy
from Strategies.path import restore
class DFS(PathFindingStrategy):
def findPath(self, maze, start, exit):
if exit is None:
return [], 0
stack = [start]
visited = {start}
parent = {}
while stack:
current = stack.pop()
if current == exit:
break
for n in maze.getNeighbors(current):
if n not in visited:
visited.add(n)
parent[n] = current
stack.append(n)
return restore(parent, start, exit), len(visited)