ZOJ 1940 Dungeon Master
2015-07-18 21:44
295 查看
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
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
Sample Output
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<cstdio> #include<iostream> #include<cstring> #include<map> #include<queue> #include<stack> #include<algorithm> #include<vector> #include<cmath> #include<string> using namespace std; const int maxn=35; int b[maxn][maxn][maxn],f[maxn][maxn][maxn],h,n,m; struct point { int x,y,z,cnt; point(int x=0,int y=0,int z=0,int cnt=0):x(x),y(y),z(z),cnt(cnt){}; bool operator ==(const point&a) { if (x==a.x&&y==a.y&&z==a.z) return true; return false; } }bg,ed,c[6]; point operator+(const point&a,const point&b) { return point(a.x+b.x,a.y+b.y,a.z+b.z,a.cnt+b.cnt); } bool check(const point& a) { if (a.x<1||a.x>h) return false; if (a.y<1||a.y>n) return false; if (a.z<1||a.z>m) return false; if (b[a.x][a.y][a.z]) return false; if (f[a.x][a.y][a.z]>=0) return false; f[a.x][a.y][a.z]=a.cnt; return true; } string s; int bfs(point &x) { int ans=0; memset(f,-1,sizeof(f)); f[x.x][x.y][x.z]=0; queue<point> p; p.push(x); while (!p.empty()) { point q=p.front(); p.pop(); if (q==ed) {ans=q.cnt; break;} for (int i=0;i<6;i++) if (check(q+c[i])) p.push(q+c[i]); } return ans; } int main() { c[0]=point(1,0,0,1); c[1]=point(-1,0,0,1); c[2]=point(0,1,0,1); c[3]=point(0,-1,0,1); c[4]=point(0,0,1,1); c[5]=point(0,0,-1,1); while (cin>>h>>n>>m,h+n+m) { for (int i=1;i<=h;i++) for (int j=1;j<=n;j++) { cin>>s; for (int k=1;k<=m;k++) { if (s[k-1]=='#') b[i][j][k]=1; else b[i][j][k]=0; if (s[k-1]=='S') bg=point(i,j,k); if (s[k-1]=='E') ed=point(i,j,k); } } int ans=bfs(bg); if (ans>0) printf("Escaped in %d minute(s).\n",ans); else printf("Trapped!\n"); } return 0; }
相关文章推荐
- 【C语言】使用递归的整数输出(高位到低位)/输出一个菱形(能够确定行数)
- boost之variant使用
- JavaScript匿名函数与闭包
- Myeclipse验证js语法报错
- 广义线性回归拟合教程和源码
- 德国精品软件推荐 压缩软件 WINRAR 个人版终于免费了。
- ScrollView
- ZOJ 1003 Crashing Balloon
- html5消除方块游戏总结-对象
- 《机器学习实战》课程笔记(第2章) k-近邻算法
- wpf 中的Canvas与Inkcanvas的属性说明
- BestCoder Round #48
- hdoj 3836 Equivalent Sets 【tarjan 求SCC + 缩点】
- 2015.7个人反思小结以及后续规划
- AYIT暑假集训大一第一周周六赛 F - 全减为1的次数
- Jar mismatch! Fix your dependencies
- POJ 3740 Easy Finding
- 剑指off-反转链表
- Codeforces Gym 100187M M. Heaviside Function two pointer
- HDU 5284 wyh2000 and a string problem(字符串,水)