2026-rff_mp/nehoroshevaa/task2/strategies/dfs_strategy.py
2026-05-24 21:49:38 +03:00

39 lines
929 B
Python

from strategies.pathfinding_strategy import PathFindingStrategy
class DFSStrategy(PathFindingStrategy):
def find_path(self, maze, start_cell, exit_cell):
stack = [start_cell]
parents = {start_cell: None}
visited = {start_cell}
visited_count = 0
while stack:
current = stack.pop()
visited_count += 1
if current == exit_cell:
path = []
while current is not None:
path.append(current)
current = parents[current]
path.reverse()
return path, visited_count
for neighbor in maze.get_neighbors(current):
if neighbor in visited:
continue
visited.add(neighbor)
parents[neighbor] = current
stack.append(neighbor)
return [], visited_count