python之迷宫BFS
# @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的更多相关文章
- ZOJ 1649 Rescue(有敌人迷宫BFS)
题意 求迷宫中从a的位置到r的位置须要的最少时间 经过'.'方格须要1s 经过'x'方格须要两秒 '#'表示墙 因为有1s和2s两种情况 须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- poj 1383 Labyrinth【迷宫bfs+树的直径】
Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4004 Accepted: 1504 Descrip ...
- hdu_1728_逃离迷宫(bfs)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意:走迷宫,找最小的拐角 题解:对BFS有了新的理解,DFS+剪枝应该也能过,用BFS就要以拐 ...
- hdu 1728 逃离迷宫 (BFS)
逃离迷宫 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
- HDU1728-逃离迷宫-BFS
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 迷宫-BFS
迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Descript ...
- HDU 1026(迷宫 BFS+打印)
题意是要穿过一个迷宫并且将每一步打印出来. 用宽搜的方法找到路径,在 vis 中存一下方向,只是这题被看到的一种不太对的运算符重载坑了很久...... 代码如下: #include <bits/ ...
- Applese走迷宫-bfs
链接:https://ac.nowcoder.com/acm/contest/330/C来源:牛客网 题目描述 精通程序设计的 Applese 双写了一个游戏. 在这个游戏中,它被困在了一个 n×mn ...
随机推荐
- linux led子系统(二)
对于led子系统中,有那么多得trigger,下面就来简单了解下. 1.default-on static void defon_trig_activate(struct led_classdev * ...
- pom.xml和testng.xml
转自:http://www.cnblogs.com/penghong2014/p/4380199.html <project xmlns="http://maven.apache.or ...
- HDOJ1002
#include <iostream>#include<stdio.h> using namespace std;int main(){ int T, count_T; cha ...
- gradle in action 笔记
原网址 https://lippiouyang.gitbooks.io/gradle-in-action-cn/content/
- all rows from client_id can grow infinitely compared to a single node when hashing by client_id
all rows from client_id can grow infinitely compared to a single node when hashing by client_id Re: ...
- Java中的final具体解释以及用途实战
浅析Java中的finalkeyword 谈到finalkeyword,想必非常多人都不陌生.在使用匿名内部类的时候可能会经经常使用到finalkeyword. 另外.Java中的String类就是一 ...
- Duplicate Observed Data
在翻看<重构-改善既有代码的设计>这本经典的书,书中就介绍了一个重构方法--Duplicate Observed Data 复制被监视数据的重构方法,使用这种方法能够使界面和对数据的操作隔 ...
- alsa和oss声音系统比较
OSS(Open Sound System) OSS的含义为,Open Sound System,是unix平台上一个统一的音频接口.以前,每个Unix厂商都会提供一个自己专有的API,用来处理音频. ...
- YTU 2451: 股市风云
2451: 股市风云 时间限制: 1 Sec 内存限制: 128 MB 提交: 37 解决: 25 [提交][状态][讨论版] 题目描述 股市强烈动荡,有涨有跌.现在有一组数据表示各公司的涨跌(涨 ...
- easyUI-右键菜单,关闭选项卡
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...