30 lines
920 B
Python
30 lines
920 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 |