forked from UNN/2026-rff_mp
52 lines
1.1 KiB
Python
52 lines
1.1 KiB
Python
from strategies.strategy import PathFindingStrategy
|
|
|
|
|
|
class DFSStrategy(PathFindingStrategy):
|
|
|
|
def findPath(self, maze, start, exit):
|
|
|
|
frontier = [start]
|
|
|
|
visited = {
|
|
start.getPosition()
|
|
}
|
|
|
|
ancestry = {}
|
|
|
|
explored_count = 0
|
|
|
|
while frontier:
|
|
|
|
current = frontier.pop()
|
|
explored_count += 1
|
|
|
|
if current == exit:
|
|
break
|
|
|
|
neighbors = maze.getNeighbors(current)
|
|
|
|
for neighbor in reversed(neighbors):
|
|
|
|
point = neighbor.getPosition()
|
|
|
|
if point in visited:
|
|
continue
|
|
|
|
visited.add(point)
|
|
ancestry[point] = current
|
|
frontier.append(neighbor)
|
|
|
|
if exit.getPosition() not in visited:
|
|
return [], explored_count
|
|
|
|
route = []
|
|
cursor = exit
|
|
|
|
while cursor != start:
|
|
route.append(cursor)
|
|
cursor = ancestry[cursor.getPosition()]
|
|
|
|
route.append(start)
|
|
route.reverse()
|
|
|
|
return route, explored_count |