基本信息
源码名称:广度优先搜索
源码大小:1.32KB
文件格式:.py
开发语言:Python
更新时间:2021-03-16
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 4 元 
   源码介绍
rom 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):

queue = deque()
queue.append((x1,y1,-1))
path = []

while len(queue) > 0:
curNode = queue.popleft()
path.append(curNode)

if curNode[0] == x2 and curNode[1] == y2:
print_r(path)
return False

for dir in dirs:
nextNode = dir(curNode[0],curNode[1])
if maze[nextNode[0]][nextNode[1]] == 0:
queue.append((nextNode[0],nextNode[1],len(path)-1))
maze[nextNode[0]][nextNode[1]] = 2
else:   
    print("没有路")
    return False


maze_path_queue(1,1,8,8)