-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.5迷宫问题- 队列.py
80 lines (58 loc) · 1.88 KB
/
03.5迷宫问题- 队列.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# https://www.bilibili.com/video/BV1uA411N7c5?p=56&spm_id_from=pageDriver&vd_source=e1de9f6d02128b9c85f5fdd03c7e72fc
'''
-广度优先搜索 同时走
使用栈路径不一定是最短的
思路:
从一个节点开始,寻找所有接下来能继续走的点,继续不断寻找直到找到出口。
使用队列存储当前正在考虑的节点
'''
from collections import deque
maze = [
[1,1,1,1,1,1,1,1,1,1],
[1,0,0,1,0,0,0,1,0,1],
[1,0,0,1,0,0,0,1,0,1],
[1,0,0,0,0,1,1,0,0,1],
[1,0,1,1,1,0,0,0,0,1],
[1,0,0,0,1,0,0,0,0,1],
[1,0,1,0,0,0,1,0,0,1],
[1,0,1,1,1,0,1,1,0,1],
[1,1,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1]
]
dirs = [
lambda x,y:(x+1,y),
lambda x,y:(x-1,y),
lambda x,y:(x,y-1),
lambda x,y:(x,y+1)
]
def print_r(path):
curNode = path[-1]
realpath=[]
while curNode[2] != -1:
realpath.append(curNode[0:2])
curNode = path[curNode[2]]
realpath.append(curNode[0:2])# 起点
realpath.reverse()
for node in realpath:
print(node)
def maze_path_queue(x1,y1,x2,y2):
queque = deque()
queque.append((x1,y1,-1))
path = []
while len(queque)>0: # 不都是死路
curNode = queque.pop()
path.append(curNode)
if curNode[0] == x2 and curNode[1] == y2:
# 终点
print_r(path)
return True
for dir in dirs:
nextNode = dir(curNode[0],curNode[1])
if maze[nextNode[0]][nextNode[1]] == 0:
queque.append((nextNode[0],nextNode[1],len(path)-1))# 后续节点进队,记录哪个节点带他来
maze[nextNode[0]][nextNode[1]] = 2# 标记为已经走过
else:
print("没有路")
return False
if __name__ == "__main__":
maze_path_queue(1,1,8,8)