2026-rff_mp/pomelovsd/ExitMaze/Strategies/DFS.py

25 lines
652 B
Python
Raw Normal View History

2026-05-23 10:37:43 +00:00
from Strategies.strat import PathFindingStrategy
2026-05-25 10:45:18 +00:00
from Strategies.path import restore
2026-05-22 20:34:48 +00:00
class DFS(PathFindingStrategy):
2026-05-23 10:37:43 +00:00
def findPath(self, maze, start, exit):
2026-05-25 13:02:23 +00:00
if exit is None:
return [], 0
2026-05-22 20:34:48 +00:00
stack = [start]
visited = {start}
parent = {}
2026-05-25 10:45:18 +00:00
while stack:
2026-05-22 20:34:48 +00:00
current = stack.pop()
if current == exit:
break
2026-05-25 10:45:18 +00:00
for n in maze.getNeighbors(current):
2026-05-22 20:34:48 +00:00
if n not in visited:
visited.add(n)
parent[n] = current
stack.append(n)
2026-05-25 13:02:23 +00:00
return restore(parent, start, exit), len(visited)