forked from UNN/2026-rff_mp
36 lines
986 B
Python
36 lines
986 B
Python
|
|
from strategies.pathfinding_strategy import PathFindingStrategy
|
||
|
|
|
||
|
|
|
||
|
|
class DFSStrategy(PathFindingStrategy):
|
||
|
|
name = "DFS"
|
||
|
|
|
||
|
|
def findPath(self, maze, start, exitCell):
|
||
|
|
self.visitedCount = 0
|
||
|
|
if start is None or exitCell is None:
|
||
|
|
return []
|
||
|
|
|
||
|
|
stack = [start]
|
||
|
|
visited = set()
|
||
|
|
parent = {}
|
||
|
|
|
||
|
|
while stack:
|
||
|
|
current = stack.pop()
|
||
|
|
pos = (current.x, current.y)
|
||
|
|
if pos in visited:
|
||
|
|
continue
|
||
|
|
|
||
|
|
visited.add(pos)
|
||
|
|
self.visitedCount += 1
|
||
|
|
|
||
|
|
if current.x == exitCell.x and current.y == exitCell.y:
|
||
|
|
return self._restore_path(parent, start, exitCell)
|
||
|
|
|
||
|
|
neighbors = maze.getNeighbors(current)
|
||
|
|
for neighbor in reversed(neighbors):
|
||
|
|
npos = (neighbor.x, neighbor.y)
|
||
|
|
if npos not in visited:
|
||
|
|
parent[npos] = current
|
||
|
|
stack.append(neighbor)
|
||
|
|
|
||
|
|
return []
|