POJ2251
2016-07-07 11:26
417 查看
//BFS找最短路径 //可以向上下左右前后六个方向搜索 #include <stdio.h> #include <iostream> #include <string.h> #include <queue> using namespace std ; int dir[6][3]= {{1,0,0},{-1,0,0},{0,-1,0},{0,1,0},{0,0,1},{0,0,-1}}; char MAP[32][32][32] ; int visit[32][32][32] ; int L , R , C ; int tl,tr,tc; int cnt; struct node { int r ; int c ; int l ; int step; }M; int BFS() { queue<node> q; int ll , rr , cc ; M.l=tl; M.r=tr; M.c=tc; M.step=0; visit[tl][tc]=1; q.push(M); while(!q.empty()) { node now; now=q.front(); q.pop(); for(int i = 0 ;i<6;i++) { ll=now.l+dir[i][0]; rr=now.r+dir[i][1]; cc=now.c+dir[i][2]; if(ll>0&&ll<=L&&rr>0&&rr<=R&&cc>0&&cc<=C&&!visit[ll][rr][cc]) { if(MAP[ll][rr][cc]=='E') { cnt=now.step+1; return cnt; } if(MAP[ll][rr][cc]=='.') { node temp; temp.l=ll; temp.r=rr; temp.c=cc; temp.step=now.step+1; visit[ll][rr][cc]=1; q.push(temp); } } } } return 0 ; } int main() { char str[10]; while(cin >> L >> R >> C) { if(!L && !R && !C) break ; for(int i = 1 ; i <= L ; i ++) { for(int j = 1 ; j <= R ; j ++) { for(int k = 1 ; k <= C ; k ++) { cin >> MAP[i][j][k] ; if(MAP[i][j][k]=='S') { tl=i; tr=j; tc=k; } } } } getchar(); getchar(); memset(visit , 0 , sizeof(visit)) ; cnt = 0 ; int ans = BFS(); if(ans==0) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",ans); } return 0 ; } 相关文章推荐
- 图算法总结
- 淘宝上拉查看图文详情效果
- Sql Server合并多行询数据到一行:使用自连接、FOR XML PATH('')、STUFF或REPLACE函数
- html
- yii2.0的mvc中的model
- 登录phpmyadmin提示: #1045 无法登录 MySQL 服务器
- 南峰子的技术博客
- dubbo + spring boot
- MongoDB 命令笔记
- UIButton 点击事件响应延迟 问题解决
- c#中的delegate(委托)和event(事件)了
- virtualbox下的centos6.8非界面系统安装以及实现虚拟机主机互通以及虚拟机联网
- jquery append html代码,click 事件失效
- 复习(java):语法:java基础库
- Zabbix之配置文件详解
- hud 1548 A strange lift
- webstrom 2016.1 破解链接
- java反射概念加案例
- NSIS FQA
- Extjs 解决表单提交总是执行failure回调函数