博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3984_bfs+回溯路径
阅读量:4553 次
发布时间:2019-06-08

本文共 1670 字,大约阅读时间需要 5 分钟。

POJ 3984  bfs+回溯路径

迷宫问题
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9218   Accepted: 5459

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 00 1 0 1 00 0 0 0 00 1 1 1 00 0 0 1 0

Sample Output

(0, 0)(1, 0)(2, 0)(2, 1)(2, 2)(2, 3)(2, 4)(3, 4)(4, 4) 找了到水题放松一下。。。竟然写了十几分钟。。。
/* poj3984 0ms  */#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;const int maxn=30;const int INF=(1<<28);const int N=5;bool vis[maxn][maxn];struct node{ int x,y; int dist;};node fa[maxn][maxn];int dx[]={-1,1,0,0};int dy[]={ 0,0,-1,1};bool bfs(){ queue
q; q.push({ 1,1,0}); vis[1][1]=1; fa[1][1]={ 0,0}; while(!q.empty()){ node now=q.front(); q.pop(); if(now.x==5&&now.y==5) return true; for(int i=0;i<4;i++){ int nx=now.x+dx[i]; int ny=now.y+dy[i]; int nd=now.dist+1; if(vis[nx][ny]) continue; vis[nx][ny]=1; fa[nx][ny]=now; q.push({nx,ny,nd}); } } return false;}void PrintPath(){ stack
path; node now={ 5,5}; while(now.x&&now.y){ path.push(now); now=fa[now.x][now.y]; } while(!path.empty()){ printf("(%d, %d)\n",path.top().x-1,path.top().y-1); path.pop(); }}int main(){ memset(vis,1,sizeof(vis)); for(int i=1;i<=N;i++){ for(int j=1;j<=N;j++){ cin>>vis[i][j]; } } if(bfs()) PrintPath(); else printf("error\n"); return 0;}
poj3984_bfs

 

转载于:https://www.cnblogs.com/--560/p/4337558.html

你可能感兴趣的文章
android ImageView scaleType属性
查看>>
day 4 继承
查看>>
14 模块
查看>>
4- 算法练习leetcode.com
查看>>
02-替换空格
查看>>
许式伟、张宴——系统架构运维思路对话
查看>>
android 左右页面滑动(滑屏)增加layout文件 而不是drawable(还有activity)
查看>>
替换textarea文本值中的换行符
查看>>
万恶的KPI、新兴的OKR及让人纠结的程序员考核
查看>>
【Win10+eclipse+MinGW+QT安装教程】已有eclipse环境下配置QT插件出错详解
查看>>
JPA + SpringData 操作数据库原来可以这么简单 ---- 深入了解 JPA - 2
查看>>
使用 Hadoop 进行语料处理(面试题)
查看>>
webmagic学习之路-1:采集安居客列表页测试
查看>>
node的consoidate的插件统一
查看>>
POj2387——Til the Cows Come Home——————【最短路】
查看>>
EPLAN标题页及图框的设计
查看>>
坐标下降法(coordinate descent method)求解LASSO的推导
查看>>
读后疑问
查看>>
实力为王 八年DBA经验谈
查看>>
More Effective C++ (静态绑定与动态类型)
查看>>