Poj3984 迷宫问题 (BFS + 路径还原)
Description
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
Output
Sample Input
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4) 找最短路挺好找,还原有点恶心了
#include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
int a[][];
const int INF=0x3f3f3f3f;
int dx[]={,,,-};
int dy[]={,-,,};
int d[][];
int sx,sy,gx,gy,nx,ny;
int res;
struct node
{
int x,y;
int prex,prey;
}path[][],tmp;
void bfs()
{
queue<node> q;
for(int i=;i<;i++){
for(int j=;j<;j++){
d[i][j]=INF;
}
}
path[sx][sy].x=sx;
path[sy][sy].y=sy;
q.push(path[sx][sy]);
d[sx][sy]=;
while(q.size()){
node p=q.front(),q.pop();
if(p.x==gx&&p.y==gy) break;
for(int i=;i<;i++){
nx=p.x+dx[i],ny=p.y+dy[i];
if(nx>=&&nx<&&ny>=&&ny<&&a[nx][ny]==&&d[nx][ny]==INF){
d[nx][ny]=d[p.x][p.y]+;
path[nx][ny].prex=p.x;
path[nx][ny].prey=p.y;
path[nx][ny].x=nx;
path[nx][ny].y=ny;
q.push(path[nx][ny]);
}
}
}
res=d[gx][gy];
}
void print_path(int x,int y)
{
if(x==&&y==){
cout<<"("<<path[][].x<<", "<<path[][].y<<")"<<endl;
return ;
}
nx=path[x][y].prex;
ny=path[x][y].prey;
print_path(nx,ny);
cout<<"("<<path[x][y].x<<", "<<path[x][y].y<<")"<<endl;
}
int main()
{
for(int i=;i<;i++){
for(int j=;j<;j++){
cin>>a[i][j];
}
}
sx=,sy=,gx=,gy=;
bfs();
print_path(,);
return ;
}
理解后自行敲一遍求总长的:
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF=0x3f3f3f3f;
int dx[]={,,,-};
int dy[]={,-,,};
int a[][];
int sx,sy,gx,gy,nx,ny;
int d[][];
int res;
int bfs()
{
queue<P> que;
//P tmp; tmp.first=sx; tmp.second=sy; que.push(tmp);
que.push(P(sx,sy));//与上面的等价
//for(int i=0;i<5;i++){
// for(int j=0;j<5;j++){
// d[i][j]=INF;
// }
//}
memset(d,INF,sizeof(d));//与上面的等价 const int INF=0x3f3f3f3f;
//memset(d,0x3f3f,sizeof(d));//与上面的等价
sx=,sy=,gx=,gy=;//设置起点终点
d[sx][sy]=;
while(que.size()){
P q=que.front();
que.pop();
if(q.first==gx&&q.second==gy){
return d[gx][gy];//到终点停止循环并返回总长
}
for(int i=;i<;i++){
nx=q.first+dx[i],ny=q.second+dy[i];
if(nx>=&&nx<&&ny>=&&ny<&&a[nx][ny]==&&d[nx][ny]==INF){
d[nx][ny]=d[q.first][q.second]+;
que.push(P(nx,ny));
}
}
}
}
int main()
{
for(int i=;i<;i++){
for(int j=;j<;j++){
cin>>a[i][j];
}
}
cout<<bfs()<<endl;//输出最短的路径总长
return ;
}
菜鸡,2019/4/22再搞一遍带还原的。。。
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <stack>
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> S;
int a[][];
int d[][];
int sx=,sy=;
int gx=,gy=;
struct node
{
int first,second;
int prefirst,presecond;
}path[][],tmp;
int dx[]={,-,,};
int dy[]={,,,-};
int nx,ny;
int x,y;
int bfs()
{
memset(d,INF,sizeof(d));
queue<node> que;
path[][].first=sx,path[][].second=sy,path[][].prefirst=,path[][].presecond=;
que.push(path[][]);
d[path[][].first][path[][].second]=;
while(!que.empty()){
node k=que.front();
que.pop();
x=k.first,y=k.second;
if(x==gx&&y==gy) break;
for(int i=;i<;i++){
nx=x+dx[i],ny=y+dy[i];
if(nx>=&&nx<&&ny>=&&ny<&&a[nx][ny]!=&&d[nx][ny]==INF){
d[nx][ny]=d[x][y]+;
path[nx][ny].first=nx;
path[nx][ny].second=ny;
path[nx][ny].prefirst=x;
path[nx][ny].presecond=y;
que.push(path[nx][ny]);
}
}
}
return d[gx][gy];
}
void print_path(int x,int y)
{
if(x==&&y==){
cout<<"("<<path[][].first<<", "<<path[][].second<<")"<<endl;
return ;
}
nx=path[x][y].prefirst;
ny=path[x][y].presecond;
print_path(nx,ny);
cout<<"("<<path[x][y].first<<", "<<path[x][y].second<<")"<<endl;
}
int main()
{
for(int i=;i<;i++){
for(int j=;j<;j++){
cin>>a[i][j];
}
}
bfs();
print_path(,);
return ;
}
Poj3984 迷宫问题 (BFS + 路径还原)的更多相关文章
- POJ-3984.迷宫问题(BFS + 路径输出)
昨天中午做的这道题,结果蛙了一整天,就因为一行代码困住了,今天算是见识到自己有多菜了.流泪.jpg 本题大意:给一个5 * 5的迷宫,1表示墙壁,0表示通路,从左上角走到右下角并输出路径. 本题思路: ...
- POJ-3894 迷宫问题 (BFS+路径还原)
定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...
- Uva 816 Abbott的复仇(三元组BFS + 路径还原)
题意: 有一个最多9*9个点的迷宫, 给定起点坐标(r0,c0)和终点坐标(rf,cf), 求出最短路径并输出. 分析: 因为多了朝向这个元素, 所以我们bfs的队列元素就是一个三元组(r,c,dir ...
- POJ3984 迷宫问题 —— BFS
题目链接:http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- HDU-4471 Yet Another Multiple Problem (BFS+路径还原)
Problem Description There are tons of problems about integer multiples. Despite the fact that the to ...
- POJ3984 迷宫问题 BFS
看题传送门:http://poj.org/problem?id=3984 BFS水一发 明天帮学弟挑电脑顺便去玩.接下来几天好好看数据结构.嗯哼. 这题标准的BFS应用,唯一需要注意的是需要输出中间的 ...
- 2019年第十届蓝桥杯省赛-迷宫(BFS/Excel大法)
这题用dfs搜不出来,需要使用bfs并记录路径,设置好方向顺序跑就ok 正解类似:POJ-3984 迷宫问题 然而毕竟是暴力杯,我们的原则是代码能省就省(懒癌晚期 于是乎网上便出现了形形色色的题解,笔 ...
- 基于JavaFX图形界面演示的迷宫创建与路径寻找
事情的起因是收到了一位网友的请求,他的java课设需要设计实现迷宫相关的程序--如标题概括. 我这边不方便透露相关信息,就只把任务要求写出来. 演示视频指路: 视频过审后就更新链接 完整代码链接: 网 ...
- ACM Computer Factory POJ - 3436 网络流拆点+路径还原
http://poj.org/problem?id=3436 每台电脑有$p$个组成部分,有$n$个工厂加工电脑. 每个工厂对于进入工厂的半成品的每个组成部分都有要求,由$p$个数字描述,0代表这个部 ...
随机推荐
- 高仿QQ、微信效果的图片浏览器(支持原图和缩略图、多种手势、CocoaPods)
感谢原文作者的分享 本文转载至 http://my.oschina.net/u/2406027/blog/735738 PYPhotoBrowser GitHub地址:https://github.c ...
- js - 伪数组转化为数组的几种方法整理(更新中...)
伪数组:无法调用数组的方法,但是有length属性,又可以索引获取内部项的数据结构. 比如:arguments.getElementsByTagName等一系列dom获取的NodeList对象,他们 ...
- apache 2.4.23 只能本地访问,其他用户不能访问,提示You don't have permission to access
这个版本的httpd.conf的配置方法跟原版本的设置不一样了. 需要在目录安全配置中 修改为 Require all granted 比如 把Require local 修改为Require al ...
- LDA总结 (一) 共轭分布
今天开始,复习一下 LDA ,记录一些 LDA 的关键步骤,为写好论文做铺垫.第一节的主题是共轭分布,回忆贝叶斯公式: \[p(\theta|X) = \frac{p(\theta) \cdot p( ...
- gym 101858
我这个傻逼被治了一下午. 大好的橘势,两个小时6T,去看L,哇傻逼题.然后我跑的最短路T到自闭 最后十几分钟去想了下A,一直在想如何表示状态..就是想不到二进制搞一下... 然后游戏结束了..以后我就 ...
- python 写入Excel
一.安装xlrd模块: 1.mac下打开终端输入命令: pip install XlsxWriter 2.验证安装是否成功: 在mac终端输入 python 进入python环境 然后输入 imp ...
- js 表达式与语句
引子:表达式和语句很基础,但是有时会犯错,比如: function(){}//报错 (function(){})//不报错 function f(x){ return x + 1 }()//报错 fu ...
- vue动态组件切换(选项卡)
vue的动态组件 <template :is='变量'></template> 可以通过改变变量,来改变template的替换内容.达到选项卡的功能 如果想要切换保持不重新创建 ...
- js获取谷歌浏览器版本 和 js分辨不同浏览器
// 获取谷歌版本 function getChromeVersion() { var arr = navigator.userAgent.split(' '); var chromeVersion ...
- python中字符串格式化的四种方法
name = "huangemiling" age= 10 address = 'nanjing' print("My name is %s,age is %d,I co ...