forked from mikepound/mazesolving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
breadthfirst.py
42 lines (31 loc) · 1.01 KB
/
breadthfirst.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
41
42
from collections import deque
def solve(maze):
start = maze.start
end = maze.end
width = maze.width
queue = deque([start])
shape = (maze.height, maze.width)
prev = [None] * (maze.width * maze.height)
visited = [False] * (maze.width * maze.height)
count = 0
completed = False
visited[start.Position[0] * width + start.Position[1]] = True
while queue:
count += 1
current = queue.pop()
if current == end:
completed = True
break
for n in current.Neighbours:
if n != None:
npos = n.Position[0] * width + n.Position[1]
if visited[npos] == False:
queue.appendleft(n)
visited[npos] = True
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]]