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代表这个部 ...
随机推荐
- git 同步远程已删除的分支和删除本地多余的分支
使用git branch -a可以查看本地分支和远程分支情况 但远程分支(红色部分)删除后,发现本地并没有同步过来. 一. 同步本地的远程分支 查看本地分支和追踪情况: git remote show ...
- 【Zookeeper系列】ZooKeeper一致性原理(转)
原文链接:https://www.cnblogs.com/sunddenly/p/4138580.html 一.ZooKeeper 的实现 1.1 ZooKeeper处理单点故障 我们知道可以通过Zo ...
- 【CF613D】Kingdom and its Cities 虚树+树形DP
[CF613D]Kingdom and its Cities 题意:给你一棵树,每次询问给出k个关键点,问做多干掉多少个非关键点才能使得所有关键点两两不连通. $n,\sum k\le 10^5$ 题 ...
- return & finally 执行顺序 这是我读到的最合理的解释
新词:return [expression] 栈顶元素 局部变量的快照 java方法是在栈幀中执行,栈幀是线程私有栈的单位,执行方法的线程会为每一个方法分配一小块栈空间来作为该方法执行时的内存空间, ...
- 小程序里let和var以及const区别
在JavaScript中有三种声明变量的方式:var.let.const. var:声明全局变量,换句话理解就是,声明在for循环中的变量,跳出for循环同样可以使用. [JavaScript] 纯文 ...
- 网站SEO优化问答精选【转载】
在接触seo的过程中,大家都会碰到很多这样或那样的问题,特别是一些seo新手由于知识有限会经常到很多地方问一些网站优化的问题,做seo时间慢慢变长之后,知识会慢慢地积累,之前的问题也会慢慢的都被解答. ...
- 实际体验Span<T> 的惊人表现
前言 最近做了一个过滤代码块功能的接口.就是获取一些博客文章做文本处理,然后这些博客文章的代码块太多了,很多重复的代码关键词如果被拿过来处理,那么会对文本的特征表示已经特征选择会有很大的影响.所以需要 ...
- Linux 安装mysql mariadb配置
CentOS 7.0 使用 yum 安装 MariaDB 与 MariaDB 的简单配置 1.安装MariaDB 安装命令 yum -y install mariadb mariadb-server ...
- J2EE快速开发框架
地址: http://git.oschina.net/blind/app 项目简介 使用Maven对项目进行模块化管理,提高项目的易开发性.扩展性. 实现了通用的系统管理模块功能,包含:用户.角色.权 ...
- springboot+@async异步线程池的配置及应用
示例: 1. 配置 @EnableAsync @Configuration public class TaskExecutorConfiguration { @Autowired private Ta ...