-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfs.py
41 lines (38 loc) · 1.27 KB
/
bfs.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
import time
class BFS:
def __init__(self, maze=None):
self.maze = maze
self.goal = (1,1)
self.directions = 'ESWN'
def solve(self):
start_time = time.time()
start = (self.maze.rows, self.maze.cols)
vis = [start]
queue = [start]
path = {}
while len(queue) > 0:
cur = queue.pop(0)
if cur == self.goal:
break
for d in self.directions:
if self.maze.maze_map[cur][d]:
if d == 'E':
Next = (cur[0], cur[1] + 1)
elif d == 'S':
Next = (cur[0] + 1, cur[1])
elif d == 'W':
Next = (cur[0], cur[1] - 1)
elif d == 'N':
Next = (cur[0] - 1, cur[1])
if Next in vis:
continue
vis.append(Next)
queue.append(Next)
path[Next] = cur
bfs_path = {}
cur = (1, 1)
while cur != start:
bfs_path[path[cur]] = cur
cur = path[cur]
end_time = time.time()
return bfs_path, vis, end_time - start_time