3D dungeon
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
2
-
描述
-
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?-
输入
-
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.
输出
-
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!
样例输入
-
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
样例输出
-
Escaped in 11 minute(s). Trapped!
//一次性搞定,颇感欣慰,不过中间有点小麻烦,输入0 0 0总是异常终止,搞半天才知道原因是我定义了一个exit变量,而exit是个函数
#include<stdio.h> #include<string.h> #include<queue> #define MAX 33 using std::queue; struct maze{ int x; int y; int z; int steps; }path,pos,start,ex; int vis[MAX][MAX][MAX]; char map[MAX][MAX][MAX]; int go_x[]={1,-1,0,0,0,0}; int go_y[]={0,0,1,-1,0,0}; int go_z[]={0,0,0,0,1,-1}; bool ok(int z,int y,int x) { if(x>=0&&x<path.x&&y>=0&&y<path.y&&z>=0&&z<path.z &&(map[z][y][x]=='.'||map[z][y][x]=='E')) return 1; return 0; } int BFS() { queue<maze>q; q.push(start); //vis[start.z][start.y][start.x]=1; while(q.size()) { struct maze temp; pos=temp=q.front(); //printf("%d %d %d %d\t",temp.z,temp.y,temp.x,temp.steps); if(pos.z==ex.z&&pos.y==ex.y&&pos.x==ex.x) return temp.steps; q.pop(); for(int i=0;i<6;i++) { temp=pos; //printf("%d %d %d %d\t",temp.x,temp.y,temp.z,temp.steps); temp.x+=go_x[i]; temp.y+=go_y[i]; temp.z+=go_z[i]; //printf("%d %d %d %d\t",temp.x,temp.y,temp.z,temp.steps); if(ok(temp.z,temp.y,temp.x)&&!vis[temp.z][temp.y][temp.x]) { //printf(" %d %d %d \t",temp.z,temp.y,temp.x); vis[temp.z][temp.y][temp.x]=1; temp.steps++; q.push(temp); } } } return -1; } int main() { while(scanf("%d%d%d",&path.z,&path.y,&path.x)!=EOF) { if(path.z==0&&path.y==0&&path.x==0) break; memset(map,0,sizeof(map)); memset(vis,0,sizeof(vis)); getchar(); for(int k=0;k<path.z;k++) { for(int j=0;j<path.y;j++) { for(int i=0;i<path.x;i++) { scanf("%c",&map[k][j][i]); if(map[k][j][i]=='S'){start.x=i;start.y=j;start.z=k;} if(map[k][j][i]=='E'){ex.x=i;ex.y=j;ex.z=k;} } getchar(); } getchar(); } //printf("%d %d %d\n",ex.z,ex.y,ex.x); int t=BFS(); if(t==-1) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",t); /* for(int k=0;k<path.z;k++) { for(int j=0;j<path.y;j++) { for(int i=0;i<path.x;i++) { printf("%c",map[k][j][i]); } printf("\n"); } printf("\n"); } */ } return 0; }
-
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).