# @File: maze_queue_bfs

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, y + 1), # 右
lambda x, y: (x + 1, y), # 下
lambda x, y: (x, y - 1), # 左
lambda x, y: (x - 1, y), # 上
] # BFS
def solve_maze_with_queue(x1, y1, x2, y2):
q = deque()
path = []
q.append((x1, y1, -1))
maze[x1][y1] = 2
while len(q) > 0:
cur_node = q.popleft()
path.append(cur_node)
if cur_node[:2] == (x2, y2):
# 终点
# for i, v in enumerate(path):
# print(i, v)
realpath = []
i = len(path) - 1
while i >= 0:
realpath.append(path[i][:2])
i = path[i][2]
realpath.reverse()
print(realpath)
return True
for d in dirs:
next_x, next_y = d(cur_node[0], cur_node[1])
if maze[next_x][next_y] == 0:
q.append((next_x, next_y, len(path) - 1)) # path列表n-1位置的点是它的父亲
maze[next_x][next_y] = 2
print(' 无路可走')
return False solve_maze_with_queue(1, 1, 8, 8)

python之迷宫BFS的更多相关文章

  1. ZOJ 1649 Rescue(有敌人迷宫BFS)

    题意 求迷宫中从a的位置到r的位置须要的最少时间  经过'.'方格须要1s  经过'x'方格须要两秒  '#'表示墙 因为有1s和2s两种情况  须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...

  2. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  3. poj 1383 Labyrinth【迷宫bfs+树的直径】

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Descrip ...

  4. hdu_1728_逃离迷宫(bfs)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意:走迷宫,找最小的拐角 题解:对BFS有了新的理解,DFS+剪枝应该也能过,用BFS就要以拐 ...

  5. hdu 1728 逃离迷宫 (BFS)

    逃离迷宫 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  6. HDU1728-逃离迷宫-BFS

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. 迷宫-BFS

    迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Descript ...

  8. HDU 1026(迷宫 BFS+打印)

    题意是要穿过一个迷宫并且将每一步打印出来. 用宽搜的方法找到路径,在 vis 中存一下方向,只是这题被看到的一种不太对的运算符重载坑了很久...... 代码如下: #include <bits/ ...

  9. Applese走迷宫-bfs

    链接:https://ac.nowcoder.com/acm/contest/330/C来源:牛客网 题目描述 精通程序设计的 Applese 双写了一个游戏. 在这个游戏中,它被困在了一个 n×mn ...

随机推荐

  1. Linux安装程序Anaconda分析(续)

    本来想写篇关于Anaconda的文章,但看到这里写的这么详细,转,原文在这里:Linux安装程序Anaconda分析(续) (1) disptach.py: 下面我们看一下Dispatcher类的主要 ...

  2. Boost源代码学习---weak_ptr.hpp

    weak_ptr是辅助shared_ptr的智能指针. 就像它的名字一样.是个"弱"指针:仅有几个接口.仅能完毕非常少工作.它能够从一个shared_ptr或weak_ptr对象构 ...

  3. springCloud和docker笔记(1)——微服务架构概述

    1.微服务设计原则 1)单一职责原则:只关注整个系统中单独.有界限的一部分(SOLID原则之一) 2)服务自治原则:具备独立的业务能力和运行环境,可独立开发.测试.构建.部署 3)轻量级通信机制:体量 ...

  4. A Practical Introduction to Blockchain with Python

    A Practical Introduction to Blockchain with Python // Adil Moujahid // Data Analytics and more http: ...

  5. 阿里Java编程规范 学习笔记

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  6. rails使用mysql数据库

    简单步骤 1,安装mysql 安裝 MySQL Ubuntu 上安裝 MySQL 請執行: $ sudo apt-get install mysql-server mysql-common mysql ...

  7. 关于div li 等标签之间自带间距

    可以用float来清除标签之间的间距. ps :ul使用font-size:0 唯一的缺点就是要再次设置LI的font-size

  8. POJ2243 Knight Moves —— A*算法

    题目链接:http://poj.org/problem?id=2243 Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  9. Collections工具类、Map集合、HashMap、Hashtable(十八)

    1.Map集合概述和特点 * A:Map接口概述 * 去重复, * 查看API可以知道, * 将键映射到值的对象, * 一个映射不能包含重复的键, * 每个键最多只能映射到一个值.* B:Map接口和 ...

  10. MYSQL进阶学习笔记九:MySQL事务的应用!(视频序号:进阶_21-22)

    知识点十:MySQL 事务的应用 (21-22) 为什么要引入事务: 为什么要引入事务这个技术呢?现在的很多软件都是多用户,多程序,多线程的.对同一表可能同时有很多人在用,为保持数据的一致性,所以提出 ...