add all strategy

This commit is contained in:
4eker 2026-05-22 23:34:48 +03:00
parent 00453eb033
commit d04a7c0c47
4 changed files with 37 additions and 0 deletions

View File

View File

View File

@ -0,0 +1,23 @@
from strat import PathFindingStrategy
from path import restore
class DFS(PathFindingStrategy):
def find_path(self, maze, start, exit):
stack = [start]
visited = {start}
parent = {}
while start:
current = stack.pop()
if current == exit:
break
for n in maze.get_neighbors(current):
if n not in visited:
visited.add(n)
parent[n] = current
stack.append(n)
return self.restore(parent, start, exit), len(visited)

View File

@ -0,0 +1,14 @@
def restore(self, parent, start, exit):
if exit not in parent and start != exit:
return[]
path = []
current = exit
while current != start:
path.append(current)
current = parent[current]
path.append(start)
path.reverse()
return path