-
Notifications
You must be signed in to change notification settings - Fork 0
/
breadthFirst.py
44 lines (32 loc) · 1.03 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
43
44
from collections import deque
def solve(maze):
print('Solving maze using breadth first...')
start = maze.start
end = maze.end
width = maze.size()
height = maze.size()
queue = deque([start])
prev = [None] * (width * height)
visited = [False] * (width * 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 is not None:
npos = n.Position[0] * width + n.Position[1]
if not visited[npos]:
queue.appendleft(n)
visited[npos] = True
prev[npos] = current
path = deque()
current = end
while current is not None:
path.appendleft(current)
current = prev[current.Position[0] * width + current.Position[1]]
return [path, [count, len(path), completed]]