ZOJ 1940 Dungeon Master-BFS-(最短路径)
来源:互联网 发布:取消淘宝账号实名认证 编辑:程序博客网 时间:2024/06/06 14:06
题意:三维空间,求起点到终点的最短路径,如果没有输出”Trapped!“
分析:
最短路径问题用bfs.
注意与分块问题的区别:计数器 t 放在node结构体里面;循环里如果a[x][y][z]="end" 返回 t+1;循环外返回 -1 来确定无解的情况
代码:
#include<iostream>#include<cstring>#include<queue>#include<cstdio>using namespace std;int l,r,c;struct node{int x,y,z;int t;};queue<node> q;char a[100][100][100];int d[6][3]={{0,1,0},{0,-1,0},{0,0,1},{0,0,-1},{1,0,0},{-1,0,0}};int bfs(){int x,y,z,t;while(!q.empty()){node tmp=q.front();x=tmp.x;y=tmp.y;z=tmp.z;t=tmp.t;q.pop();for(int i=0;i<6;i++){int dx=x+d[i][1];int dy=y+d[i][2];int dz=z+d[i][0];if(dx>=0&&dz<l&&dy>=0&&dx<r&&dz>=0&&dy<c&&a[dz][dx][dy]!='#'){if(a[dz][dx][dy]=='E') return t+1;a[dz][dx][dy]='#';node tmp;tmp.x=dx;tmp.y=dy;tmp.z=dz;tmp.t=t+1;q.push(tmp);}}}return -1;}int main(){while(cin>>l>>r>>c){if(!l&&!r&&!c) break;while(!q.empty()) q.pop();for(int i=0;i<l;i++) for(int j=0;j<r;j++){ scanf("%s",a[i][j]); for(int k=0;k<c;k++) { if(a[i][j][k]=='S'){ node tmp; tmp.x=j; tmp.y=k; tmp.z=i; tmp.t=0; q.push(tmp); a[i][j][k]='#'; } } }int tmp=bfs();if(tmp==-1) cout<<"Trapped!"<<endl;else cout<<"Escaped in "<<tmp<<" minute(s)."<<endl;}}
0 0
- ZOJ 1940 Dungeon Master-BFS-(最短路径)
- ZOJ 1940 Dungeon Master 【BFS】
- zoj 1940 Dungeon Master(BFS)
- ZOJ 1940 Dungeon Master bfs
- 算法训练之BFS POJ 2251 Dungeon Master 三维最短路径
- zoj 1940 || poj 2251 Dungeon Master(bfs)
- POJ 2251 Dungeon Master 三维最短路径
- ZOJ 1940 Dungeon Master
- ZOJ 1940 Dungeon Master
- zoj 1940 Dungeon Master
- zoj - 1940 - Dungeon Master
- zoj 1940 Dungeon Master
- zoj 1940 Dungeon Master
- ZOJ 1940 Dungeon Master
- ZOJ 1940 Dungeon Master
- ZOJ 1940 Dungeon Master
- zoj 1940 Dungeon Master
- Dungeon Master(bfs)
- 我不是我自己,我怎么可能快乐
- const_cast引发的常量折叠思考
- java web在线预览pdf
- android之XListview,上拉下拉出现BUG
- linux 信号之SIGNAL 0
- ZOJ 1940 Dungeon Master-BFS-(最短路径)
- iOS TextField输入框点击键盘时随着键盘上移
- HDU 2545 树上战争
- Windows系统Eclipse的copy lines默认快捷键Ctrl+alt+down不可用
- Android入门(54)——第九章 使用GestureOverlayView进行手势识别
- nginx日志文件切割
- pthreads 2.0.10 test
- 原型模式的浅度克隆和深度克隆的实现
- 基于jquery的秒表倒计时