poj 2251 Dungeon Master
2015-06-22 18:10
337 查看
Dungeon Master
Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon 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 the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Sample Input
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 19222 | Accepted: 7466 |
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon 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 the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
#include<stdio.h> #include<string.h> #include<queue> #define MAX 288 using namespace std; int l,r,c; char map[MAX][MAX][MAX]; struct node { int x,y,z; int step; }; void bfs(int x1,int y1,int z1,int x2,int y2,int z2) { int j,i,ok=0; int move[6][3]={0,0,1,0,0,-1,1,0,0,-1,0,0,0,1,0,0,-1,0}; queue<node>q; node beg,end; beg.x=x1; beg.y=y1; beg.z=z1; beg.step=0; q.push(beg); while(!q.empty()) { end=q.front(); q.pop(); if(end.x==x2&&end.y==y2&&end.z==z2) { ok=1; break; } for(i=0;i<6;i++) { beg.x=end.x+move[i][0]; beg.y=end.y+move[i][1]; beg.z=end.z+move[i][2]; if(0<=beg.x&&beg.x<l&&0<=beg.y&&beg.y<r&&0<=beg.z&&beg.z<c&&map[beg.x][beg.y][beg.z]!='#') { map[beg.x][beg.y][beg.z]='#'; beg.step=end.step+1; q.push(beg); } } } if(ok) printf("Escaped in %d minute(s).\n",end.step); else printf("Trapped!\n"); } int main() { int i,j,k,x1,y1,z1,x2,y2,z2; while(scanf("%d%d%d",&l,&r,&c)&&l!=0&&r!=0&&c!=0) { for(i=0;i<l;i++) { for(j=0;j<r;j++) { scanf("%s",map[i][j]); } } for(i=0;i<l;i++) for(j=0;j<r;j++) for(k=0;k<c;k++) { if(map[i][j][k]=='S') { x1=i;y1=j;z1=k; } else if(map[i][j][k]=='E') { x2=i,y2=j;z2=k; } } bfs(x1,y1,z1,x2,y2,z2); } return 0; }
相关文章推荐
- ubuntu14.10安装jdk
- FireBug使用总结
- 《你的灯还亮着吗》读后感3
- Magento订单取消的时候还原优惠券的使用次数(Single Coupon marked as used when payment pending)
- 【最大流】【HDU2883】【kebab】
- 【最大流】【HDU2883】【kebab】
- 属性readwrite,readonly,assign,retain,copy,nonatomic
- Autoconf/Automake工具简介
- 一个线程写完数据后,另外三个线程按顺序读取
- [转]Loadrunner随机生成15位数字串
- JAVA的Random类
- 《大道至简》3
- tcp异常断开的重连解决方法
- LZMA C# SDK 结合 UPK 打包压缩 多文件夹 Unity3d实例
- 搬移网站程序到不同服务器的操作流程
- Quagga的no ospf_interface问题
- Unity+NGUI打造网络图片异步加载和本地缓存工具(一)
- 《你的灯还亮着吗》读后感2
- 黑马程序员--Java学习日记之eclipse开发工具的熟练使用,磨刀不误砍柴工!
- Zynq 在Ubuntu上搭建编译环境