Python之路——迷宫问题

用堆来解决

 1 maze = [
 2     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
 3     [1, 0, 0, 1, 0, 0, 0, 1, 0, 1],
 4     [1, 0, 0, 1, 0, 0, 0, 1, 0, 1],
 5     [1, 0, 0, 0, 0, 1, 1, 0, 0, 1],
 6     [1, 0, 1, 1, 1, 0, 0, 0, 0, 1],
 7     [1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
 8     [1, 0, 1, 0, 0, 0, 1, 0, 0, 1],
 9     [1, 0, 1, 1, 1, 0, 1, 1, 0, 1],
10     [1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
11     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
12 ]
13 
14 dirs = [
15     lambda x,y:(x+1,y),#
16     lambda x,y:(x-1,y),#
17     lambda x,y:(x,y-1),#
18     lambda x,y:(x,y+1),#
19 ]
20 
21 def maze_path(x1,y1,x2,y2):
22     stack = []
23     stack.append((x1,y1))
24     while(len(stack)>0):
25         curNode = stack[-1] # 当前的节点
26         if curNode[0]==x2 and curNode[1]==y2:
27             # 走到终点了
28             for p in stack:
29                 print(p)
30             return True
31         # x,y 四个方向:上 x-1,y, 右 x,y+1, 下 x+1,y, 左 x,y-1
32         for dir in dirs:
33             nextNode = dir(curNode[0],curNode[1])
34             if maze[nextNode[0]][nextNode[1]] == 0:
35                 stack.append(nextNode)
36                 maze[nextNode[0]][nextNode[1]] = 2
37                 break
38         else:
39             stack.pop()
40     else:
41         print('没有路')
42         return False
43 maze_path(1,1,8,8)

用队列来解决

 1 from collections import deque
 2 
 3 maze = [
 4     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
 5     [1, 0, 0, 1, 0, 0, 0, 1, 0, 1],
 6     [1, 0, 0, 1, 0, 0, 0, 1, 0, 1],
 7     [1, 0, 0, 0, 0, 1, 1, 0, 0, 1],
 8     [1, 0, 1, 1, 1, 0, 0, 0, 0, 1],
 9     [1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
10     [1, 0, 1, 0, 0, 0, 1, 0, 0, 1],
11     [1, 0, 1, 1, 1, 0, 1, 1, 0, 1],
12     [1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
13     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
14 ]
15 
16 dirs = [
17     lambda x,y:(x+1,y),#
18     lambda x,y:(x,y-1),#
19     lambda x,y:(x,y+1),#
20     lambda x,y:(x-1,y),#
21 
22 ]
23 
24 def print_r(path):
25     curNode = path[-1]
26     realpath = []
27     while curNode[2] != -1:
28         realpath.append(curNode[0:2])
29         curNode = path[curNode[2]]
30     realpath.append(curNode[0:2]) # 把起点放进去
31     realpath.reverse()
32     print(len(realpath))
33     for i in realpath:
34         print(i)
35 
36 def maze_path_queue(x1,y1,x2,y2):
37     maze[x1][y1]=2
38     queue = deque()
39     queue.append((x1,y1,-1))
40     path = []
41     while len(queue)>0:
42         curNode = queue.popleft()
43         path.append(curNode)
44         if curNode[0] == x2 and curNode[1] == y2:
45             print_r(path)
46             return True
47         for dir in dirs:
48             nextNode = dir(curNode[0],curNode[1])
49             if maze[nextNode[0]][nextNode[1]]==0:
50                 # 后续节点进队,记录哪个节点带他来的
51                 node = (nextNode[0],nextNode[1],len(path)-1)
52                 queue.append(node)
53                 maze[nextNode[0]][nextNode[1]] = 2  # 标记为已经走过
54     else:
55         print('没有路')
56         return False
57 maze_path_queue(1,1,8,8)

 

posted @ 2018-02-01 20:43  liuyankui163  阅读(3329)  评论(0编辑  收藏  举报