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

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

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 + 路径还原)的更多相关文章

  1. POJ-3984.迷宫问题(BFS + 路径输出)

    昨天中午做的这道题,结果蛙了一整天,就因为一行代码困住了,今天算是见识到自己有多菜了.流泪.jpg 本题大意:给一个5 * 5的迷宫,1表示墙壁,0表示通路,从左上角走到右下角并输出路径. 本题思路: ...

  2. 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, ...

  3. Uva 816 Abbott的复仇(三元组BFS + 路径还原)

    题意: 有一个最多9*9个点的迷宫, 给定起点坐标(r0,c0)和终点坐标(rf,cf), 求出最短路径并输出. 分析: 因为多了朝向这个元素, 所以我们bfs的队列元素就是一个三元组(r,c,dir ...

  4. POJ3984 迷宫问题 —— BFS

    题目链接:http://poj.org/problem?id=3984 迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  5. HDU-4471 Yet Another Multiple Problem (BFS+路径还原)

    Problem Description There are tons of problems about integer multiples. Despite the fact that the to ...

  6. POJ3984 迷宫问题 BFS

    看题传送门:http://poj.org/problem?id=3984 BFS水一发 明天帮学弟挑电脑顺便去玩.接下来几天好好看数据结构.嗯哼. 这题标准的BFS应用,唯一需要注意的是需要输出中间的 ...

  7. 2019年第十届蓝桥杯省赛-迷宫(BFS/Excel大法)

    这题用dfs搜不出来,需要使用bfs并记录路径,设置好方向顺序跑就ok 正解类似:POJ-3984 迷宫问题 然而毕竟是暴力杯,我们的原则是代码能省就省(懒癌晚期 于是乎网上便出现了形形色色的题解,笔 ...

  8. 基于JavaFX图形界面演示的迷宫创建与路径寻找

    事情的起因是收到了一位网友的请求,他的java课设需要设计实现迷宫相关的程序--如标题概括. 我这边不方便透露相关信息,就只把任务要求写出来. 演示视频指路: 视频过审后就更新链接 完整代码链接: 网 ...

  9. ACM Computer Factory POJ - 3436 网络流拆点+路径还原

    http://poj.org/problem?id=3436 每台电脑有$p$个组成部分,有$n$个工厂加工电脑. 每个工厂对于进入工厂的半成品的每个组成部分都有要求,由$p$个数字描述,0代表这个部 ...

随机推荐

  1. 本人SW知识体系导航 - Programming menu

    将感悟心得记于此,重启程序员模式. js, py, c++, java, php 融汇之全栈系列 [Full-stack] 快速上手开发 - React [Full-stack] 状态管理技巧 - R ...

  2. Android 官方独立 adb / fastboot 工具包

    https://dl.google.com/android/repository/platform-tools-latest-darwin.zip https://dl.google.com/andr ...

  3. cc攻击和ddos攻击

    DoS攻击.CC攻击的攻击方式和防御方法 DDoS介绍 DDoS是英文Distributed Denial of Service的缩写,意即“分布式拒绝服务”,那么什么又是拒绝服务(Denial of ...

  4. [Codis] Codis3部署流程

    #0 前言 最近因为项目需要,研究了一下传说中的Codis.下面跟大家分享Codis3的搭建流程 https://github.com/CodisLabs/codis #1 Codis是什么 官方的介 ...

  5. JDK 5.0 注解知识快速进阶

    1.了解注解 对于Java开发人员来说,在编写代码时,除了源程序外,还会使用Javadoc标签对类.方法或成员变量进行注释,一遍使用Javadoc工具生成和源代码配套的Javadoc文件,如@para ...

  6. 微信小程序开发笔记01

    微信小程序开发的优势 1,不用安装,即开即用,用完就走.省流量,省安装时间,不占用桌面: 2,体验上虽然没法完全媲美原生APP,但综合考虑还是更优: 3,对于小程序拥有者来说,开发成本更低,他们可以更 ...

  7. juqery 点击分页显示,指定一页显示多少个,首次加载显示多少个

    源代码html: //源代码:html <div class="jq22"> <div class="hidden"> <li&g ...

  8. 树状数组 || 线段树 || Luogu P5200 [USACO19JAN]Sleepy Cow Sorting

    题面:P5200 [USACO19JAN]Sleepy Cow Sorting 题解: 最小操作次数(记为k)即为将序列倒着找第一个P[i]>P[i+1]的下标,然后将序列分成三部分:前缀部分( ...

  9. F#周报2019年第3期

    新闻 SAFE最近的活动 什么开源项目适合我们的奖学金受益者上手工作 布署SAFE应用至Google Cloud AppEngine Alea GPU:使用F#进行GPU编程 Rider 2018.3 ...

  10. Gym 101194C / UVALive 7899 - Mr. Panda and Strips - [set][2016 EC-Final Problem C]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...