poj 2251 Dungeon Master (三维bfs)
2015-07-21 13:02
447 查看
http://poj.org/problem?id=2251
简单bfs,只不过是三维的。。。
唯一的坑点在输出上...
另:终于找到了好的读地图的方法。。。而不用担心回车符。
就是先读成字符串。
具体见代码
简单bfs,只不过是三维的。。。
唯一的坑点在输出上...
Escaped in %d minute(s) 这意思是答案为1输出minute,不为1输出minutes还是说是不是1都输出minute(s)? 试了下,答案是后者。
另:终于找到了好的读地图的方法。。。而不用担心回车符。
就是先读成字符串。
具体见代码
/************************************************************************* > File Name: code/2015summer/searching/B.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年07月17日 星期五 16时47分46秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> using namespace std; #define REP(i, n) for (int i=0;i<int(n);++i) typedef long long LL; typedef unsigned long long ULL; const int N=40; char st ; int d ; int l,r,c; int sx,sy,sz,tx,ty,tz; int dirx[6]={1,-1,0,0,0,0}; int diry[6]={0,0,-1,1,0,0}; int dirz[6]={0,0,0,0,1,-1}; bool ok(int x,int y,int z) { if (z>=0&&z<l&&x>=0&&x<r&&y>=0&&y<c&&d[z][x][y]==-1&&st[z][x][y]!='#') return true; return false; } void bfs() { memset(d,-1,sizeof(d)); queue<int>x; queue<int>y; queue<int>z; x.push(sx); y.push(sy); z.push(sz); d[sz][sx][sy]=0; while (!x.empty()&&!y.empty()&&!z.empty()) { int prex = x.front();x.pop(); int prey = y.front();y.pop(); int prez = z.front();z.pop(); for ( int i = 0 ; i < 6 ; i++ ) { int newx = prex+dirx[i]; int newy = prey+diry[i]; int newz = prez+dirz[i]; bool flag = ok(newx,newy,newz); if (flag) { d[newz][newx][newy]=d[prez][prex][prey]+1; x.push(newx); y.push(newy); z.push(newz); } } } } int main() { while (scanf("%d %d %d",&l,&r,&c)) { if (l==0&&r==0&&c==0) break; for ( int i = 0 ; i < l ; i++) { for ( int j = 0 ; j < r; j++) scanf("%s",st[i][j]); } for ( int i = 0 ; i < l ; i++ ) { for ( int j = 0 ; j < r ; j ++) { for ( int k = 0 ; k < c ; k++ ) { if (st[i][j][k]=='S') { sx = j; sy = k; sz = i; } if (st[i][j][k]=='E') { tx = j; ty = k; tz = i; } } } } bfs(); int ans = d[tz][tx][ty]; if (ans==-1) { printf("Trapped!\n"); } else { // if (ans==1) // printf("Escaped in 1 minute.\n"); // else printf("Escaped in %d minutes.\n",ans); printf("Escaped in %d minute(s).\n",ans); } } }
相关文章推荐
- Fedora22安装搜狗输入法
- Android开发之启动Activity的最佳写法
- [python-1]数据类型、控制流语句、模块、函数和类
- 安卓实习第五天
- 《Effective C++》:规定44-规定45
- Borg Maze - poj 3026(BFS + Kruskal 算法)
- c# 封装,继承,多态
- Android 开发之 HeartProgress 自定义心形进度条
- PHP操作Mysql中间BLOB场
- Reset CSS杂烩
- TI BLE开发(一)修改蓝牙广播间隔
- iBatis框架使用 4步曲
- 内存分配栈和堆的区别
- Latex 多个参考文献的引用以及文献字体大小调整方法
- Spring JdbcTemplate 使用总结与经验分享
- HDU - 1693 Eat the Trees(多回路插头DP)
- spring框架学习(六)AOP
- spring框架学习(五)注解
- cp在复制文件的时候被提示确认覆盖
- spring框架学习(四)自动装配