Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18032 | Accepted: 6995 |
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
Sample Output
Escaped in 11 minute(s). Trapped!
题意:
给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径
移动方向可以是上,下,左,右,前,后,六个方向
每移动一次就耗费一分钟,要求输出最快的走出时间。
不同L层的地图,相同RC坐标处是连通的
三维Bfs,Dfs超时了,都贴出来随便吐槽吧;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct node{
int x;
int y;
int z;
int step;
};
char Map[35][35][35];
int vis[35][35][35];
int l,r,c;
int ans;
int dx[]={-1,1,0,0,0,0};
int dy[]={0,0,-1,1,0,0};
int dz[]={0,0,0,0,-1,1};
int ok(int x,int y,int z){
if(x>=0 && x<l && y>=0 && y<r && z>=0 && z<c)
return 1;
else
return 0;
}
//void Dfs(int x,int y,int z,int step){
// if(Map[z][x][y]=='E'){
// ans=min(ans,step);
// return ;
// }
// for(int i=0;i<6;i++){
// if( ok( x+dx[i] , y+dy[i] , z+dz[i] ) ){
// vis[ z+dz[i] ][ x+dx[i] ][ y+dy[i] ]=1;
// Dfs( x+dx[i] , y+dy[i] , z+dz[i] , step+1 );
// vis[ z+dz[i] ][ x+dx[i] ][ y+dy[i] ]=0;
// }
// }
//}
void Bfs(int x,int y,int z){
queue<node> q;
node p;
p.x=x;
p.y=y;
p.z=z;
p.step=0;
q.push(p);
vis[x][y][z]=1;
while(!q.empty()){
p=q.front();
q.pop();
for(int i=0;i<6;i++){
node s;
s=p;
s.x+=dx[i];
s.y+=dy[i];
s.z+=dz[i];
if( ok( s.x,s.y,s.z ) && Map[s.x][s.y][s.z]!='#' && !vis[s.x][s.y][s.z]){
if(Map[s.x][s.y][s.z]=='E'){
ans=p.step+1;
return ;
}
vis[s.x][s.y][s.z]=1;
s.step++;
q.push(s);
}
}
}
}
int main(){
while(scanf("%d%d%d",&l,&r,&c)!=EOF){
int x,y,z;
if(l==0&&r==0&&c==0)
break;
for(int k=0;k<l;k++){
for(int i=0;i<r;i++){
scanf("%s",Map[k][i]);
for(int j=0;j<c;j++){
if(Map[k][i][j]=='S'){
x=k;
y=i;
z=j;
}
}
}
}
ans=-1;
memset(vis,0,sizeof(vis));
Bfs(x,y,z);
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}