Dungeon Master(bfs)
来源:互联网 发布:初中语文软件哪个好 编辑:程序博客网 时间:2024/06/05 11:07
E - DungeonMaster
TimeLimit:1000MS Submit Status Practice POJ2251
Description
You are trapped in a 3D dungeon and need to findthe quickest way out! The dungeon is composed of unit cubes whichmay or may not be filled with rock. It takes one minute to move oneunit north, south, east, west, up or down. You cannot movediagonally and the maze is surrounded by solid rock on allsides.
Is an escape possible? If yes, how long will ittake?
Is an escape possible? If yes, how long will ittake?
Input
The input consists of a number of dungeons. Eachdungeon description starts with a line containing three integers L,R and C (all limited to 30 in size).
L is the number of levels making up thedungeon.
R and C are the number of rows and columns making up the plan ofeach level.
Then there will follow L blocks of R lines each containing Ccharacters. Each character describes one cell of the dungeon. Acell full of rock is indicated by a '#' and empty cells arerepresented by a '.'. Your starting position is indicated by 'S'and the exit by the letter 'E'. There's a single blank line aftereach level. Input is terminated by three zeroes for L, R andC.
L is the number of levels making up thedungeon.
R and C are the number of rows and columns making up the plan ofeach level.
Then there will follow L blocks of R lines each containing Ccharacters. Each character describes one cell of the dungeon. Acell full of rock is indicated by a '#' and empty cells arerepresented by a '.'. Your starting position is indicated by 'S'and the exit by the letter 'E'. There's a single blank line aftereach level. Input is terminated by three zeroes for L, R andC.
Output
Each maze generates one line of output. If it ispossible to reach the exit, print a line of theform
where x is replaced by the shortest time it takes toescape.
If it is not possible to escape, print theline
Escaped in x minute(s).
where x is replaced by the shortest time it takes toescape.
If it is not possible to escape, print theline
Trapped!
Sample Input
3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Sample Output
Escaped in 11 minute(s).Trapped!
#include#include#include#includeusing namespace std;int dir[2]={1,-1};char arr[30][30][30];int l,r,c;struct point{ int row,col,level; int step;}p;int bfs(point s,point e){ int map[r][c][l];memset(map,0,sizeof(map)); queue que; point p; int x=0,y=0,z=0; while(true) {if(s.col==e.col&&s.level==e.level&&s.row==e.row){return s.step;}for(inti=0;i<6;i++){if(i<2){x=s.col+dir[i];y=s.row;z=s.level;}else if(i>=2&&i<4){y=s.row+dir[i%2];x=s.col;z=s.level;}else if(i>=4&&i<6){z=s.level+dir[i%2];x=s.col;y=s.row;}if(x<0||x>r-1||y<0||y>c-1||z<0||z>l-1)continue;if((!map[x][y][z])&&(arr[x][y][z]=='.'||arr[x][y][z]=='E')){p.col=x;p.level=z;p.row=y;map[x][y][z]=1;p.step=s.step+1;que.push(p);}}if(que.empty())return 0;s=que.front();que.pop(); }}int main(){ point s; point e;while(cin>>l>>r>>c){if(l==0&&r==0&&c==0){break;}for(int k=0;k{for(inti=0;i{for(int j=0;j{cin>>arr[i][j][k];if(arr[i][j][k]=='S'){s.col=i;s.row=j;s.level=k;s.step=0;}if(arr[i][j][k]=='E'){e.col=i;e.row=j;e.level=k;e.step=0;}}}}int minute = bfs(s,e);if(minute==0)cout<<"Trapped!"<<endl;else{cout<<"Escaped in "<<minute<<"minute(s)."<<endl;}} return 0;}
0 0
- poj2251 Dungeon Master (BFS)
- (BFS)2251 Dungeon Master
- Dungeon Master bfs
- 532 - Dungeon Master//bfs
- Dungeon Master(poj2251,bfs)
- Dungeon Master(bfs)
- Dungeon Master BFS
- POJ2251:Dungeon Master(BFS)
- poj2251 Dungeon Master bfs
- Dungeon Master uva BFS
- POJ2251:Dungeon Master(BFS)
- toj1140. Dungeon Master (bfs)
- poj2251 Dungeon Master(BFS)
- POJ2251 Dungeon Master BFS
- Dungeon Master-BFS
- [bfs]poj2251 Dungeon Master
- poj2251 dungeon master【BFS】~
- 2251 Dungeon Master【bfs】
- 计算器(calculator ;counter)一般是指“电子计算器”
- EntityManager使用方法
- Android 4.4特性说明(持续更新)
- hibernate注解的CascadeType属性
- java.lang.NoSuchFieldError:NONE
- Dungeon Master(bfs)
- struct和typedef struct
- 中缀表达式转后缀表达式
- 表达式求值(中缀)
- 我已率先抢占昵称,我的昵称独一无…
- hanoi问题详解
- 使用xcode4.2检测内存泄露leak的具体位置的两种方法总结 .
- 算法竞赛入门经典 习题2-10
- stl算法:next_permutation剖析