Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16860 | Accepted: 6567 |
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?
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.
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
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
这是挂在POJ训练计划DFS中的一道题,用DFS做了很久都运行不对,听别人说这个题用BFS做 sad~ 结果BFS一遍就过了(DFS还是理解的不深。。。。。)
一个BFS水题,给你一个三维的图,求从S出发,到E的最短步数,其中#不能走,走不到E输出Trapped!
#include <iostream> #include <cstdio> #include <cstring> #include <stdlib.h> using namespace std; struct node { int x,y,z; int sum; }q[100001]; int s,n,m; char map[31][31][31]; int b[31][31][31]; int xx[]={0, 0,0, 0,1,-1}; int yy[]={1,-1,0, 0,0, 0}; int zz[]={0, 0,1,-1,0, 0}; void bfs(int x,int y,int z) { node t,r; t.x=x; t.y=y; t.z=z; t.sum=0; int k=0,l=0; q[l++]=t; while(k<l) { t=q[k++]; if(map[t.x][t.y][t.z]=='E') { printf("Escaped in %d minute(s).\n",t.sum); return ; } for(int i=0;i<6;i++) { r.x=t.x+xx[i]; r.y=t.y+yy[i]; r.z=t.z+zz[i]; if(r.x>=0 && r.x<s && r.y>=0 && r.y<n && r.z>=0 && r.z<m && map[r.x][r.y][r.z]!='#'&&!b[r.x][r.y][r.z]) { b[r.x][r.y][r.z]=1; r.sum=t.sum+1; q[l++]=r; } } } printf("Trapped!\n"); } int main() { while(~scanf("%d%d%d",&s,&n,&m)) { getchar(); if(!s&&!n&&!m) break; char a[100]; memset(b,0,sizeof(b)); for(int i=0;i<s;i++) { for(int j=0;j<n;j++) scanf("%s",map[i][j]); gets(a); } for(int i=0;i<s;i++) { for(int j=0;j<n;j++) for(int k=0;k<m;k++) { if(map[i][j][k]=='S') { bfs(i,j,k); break; } } } } return 0; }