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代表这个部 ...
随机推荐
- python利用opencv合成模糊图像
之前需要评估图像质量来筛选成像质量不错的图片,去除由于对焦,运动等造成的模糊图像,所以在构建数据集的时候考虑用opencv对清晰的图片进行处理获得模糊的图片从而进行训练. 1) 运动模糊图像 一般来说 ...
- 我的海外购页面List
<%@ page language="java" contentType="text/html;charset=UTF-8" %> <%@ t ...
- [转]解决Error: That port is already in use.
ubuntu系统下,运行一个django项目,即输入python manage.py runserver后,可能出现 Error: That port is already in use.的错误. 即 ...
- go语言学习笔记1 Go开发环境
什么是Go?Go是一门并发支持.垃圾回收的编译型系统编程语言,旨在创造一门具有在静态编译语言的高性能和动态语言的高效开发之间拥有良好平衡的一门编程语言. Go的主要特点有哪些?* 类型安全 和 内存安 ...
- IDEA-各模块间引用出现问题的解决方法
1 点击项目右上角的Project Structure 2 选择Modules->父项目->点击右上角的加号->添加需要依赖的模块
- VSCode远程调试Go程序方法(Attach)
set launch.json { "name": "Attach", "type": "go", "requ ...
- 如何写Emit代码
写Emit代码也不是想象中的那么复杂,基本过程就是先手工写好C#代码,编译得到Exe或者Dll,然后用ILDASM或反编译工具,得到IL代码,最后就是看着IL代码,用Emit一个个对应发出代码,就行了 ...
- Ubuntu 离线安装Mysql
一.安装包 先从网络上,下载Mysql安装包,复制到U盘 下载地址:https://dev.mysql.com/downloads/mysql/ 二.挂载U盘 2.1查看分区 先输入命令 cat ...
- 二、JavaScript基础(2)
BOM基础加强 1.浏览器对象BOM DOM Window DOM Navigator DOM Screen DOM History DOM Location 2.浏览器对象的使用 History H ...
- phpredis Redis阵列 Redis Arrays
官方URL:https://github.com/phpredis/phpredis/blob/master/arrays.markdown#readme 2017年10月29日20:44:01 Re ...