add all strategy
This commit is contained in:
parent
00453eb033
commit
d04a7c0c47
0
pomelovsd/ExitMaze/Strategies/A*.py
Normal file
0
pomelovsd/ExitMaze/Strategies/A*.py
Normal file
0
pomelovsd/ExitMaze/Strategies/BFS.py
Normal file
0
pomelovsd/ExitMaze/Strategies/BFS.py
Normal file
23
pomelovsd/ExitMaze/Strategies/DFS.py
Normal file
23
pomelovsd/ExitMaze/Strategies/DFS.py
Normal 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)
|
||||
14
pomelovsd/ExitMaze/Strategies/path.py
Normal file
14
pomelovsd/ExitMaze/Strategies/path.py
Normal 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
|
||||
Loading…
Reference in New Issue
Block a user