forked from mikepound/mazesolving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
depthfirst.py
40 lines (32 loc) · 1.03 KB
/
depthfirst.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from collections import deque
def solve(maze):
start = maze.start
end = maze.end
width = maze.width
stack = deque([start])
shape = (maze.height, maze.width)
prev = [None] * (maze.width * maze.height)
visited = [False] * (maze.width * maze.height)
count = 0
completed = False
while stack:
count += 1
current = stack.pop()
if current == end:
completed = True
break
visited[current.Position[0] * width + current.Position[1]] = True
#import code
#code.interact(local=locals())
for n in current.Neighbours:
if n != None:
npos = n.Position[0] * width + n.Position[1]
if visited[npos] == False:
stack.append(n)
prev[npos] = current
path = deque()
current = end
while (current != None):
path.appendleft(current)
current = prev[current.Position[0] * width + current.Position[1]]
return [path, [count, len(path), completed]]