Dungeon Master
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 26176 | Accepted: 10198 |
Description
You are trapped in a 3D
dungeon(地牢) and need to find the quickest way out! The dungeon iscomposed(构成) 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 themaze(迷宫) 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 threeintegers(整数)
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 isindicated(表明) 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 isindicated(表明) 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 ofoutput(输出). 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!#include<stdio.h> #include<string.h> #include<queue> using namespace std; struct node { int x,y,z; int step; }tmp1,tmp2; char map[50][50][50]; int vis[50][50][50]; int n,m,t; int main() { int i,j,k; int z,x,y; struct node start; while(~scanf("%d%d%d",&z,&x,&y)) { memset(vis,0,sizeof(vis)); memset(map,0,sizeof(map)); if(z==0&&x==0&&y==0) break; for(i=0;i<z;i++) for(j=0;j<x;j++) { scanf("%s",map[i][j]); for(k=0;k<y;k++) { if(map[i][j][k]=='S') { start.x=j; start.y=k; start.z=i; start.step=0; vis[i][j][k]=1; break; } } } queue<node>q; q.push(start); int flag = 0; while(!q.empty()) { tmp1=q.front(); q.pop(); if(map[tmp1.z][tmp1.x][tmp1.y]=='E') { flag=1; printf("Escaped in %d minute(s).\n",tmp1.step); break; } tmp1.step++; if(tmp1.x>0) { tmp2=tmp1; tmp2.x--; if(!vis[tmp2.z][tmp2.x][tmp2.y]&&map[tmp2.z][tmp2.x][tmp2.y]!='#') { vis[tmp2.z][tmp2.x][tmp2.y]=1; q.push(tmp2); } } if(tmp1.x<x-1) { tmp2=tmp1; tmp2.x++; if(!vis[tmp2.z][tmp2.x][tmp2.y]&&map[tmp2.z][tmp2.x][tmp2.y]!='#') { vis[tmp2.z][tmp2.x][tmp2.y]=1; q.push(tmp2); } } if(tmp1.y>0) { tmp2=tmp1; tmp2.y--; if(!vis[tmp2.z][tmp2.x][tmp2.y]&&map[tmp2.z][tmp2.x][tmp2.y]!='#') { vis[tmp2.z][tmp2.x][tmp2.y]=1; q.push(tmp2); } } if(tmp1.y<y-1) { tmp2=tmp1; tmp2.y++; if(!vis[tmp2.z][tmp2.x][tmp2.y]&&map[tmp2.z][tmp2.x][tmp2.y]!='#') { vis[tmp2.z][tmp2.x][tmp2.y]=1; q.push(tmp2); } } if(tmp1.z>0) { tmp2=tmp1; tmp2.z--; if(!vis[tmp2.z][tmp2.x][tmp2.y]&&map[tmp2.z][tmp2.x][tmp2.y]!='#') { vis[tmp2.z][tmp2.x][tmp2.y]=1; q.push(tmp2); } } if(tmp1.z<z-1) { tmp2=tmp1; tmp2.z++; if(!vis[tmp2.z][tmp2.x][tmp2.y]&&map[tmp2.z][tmp2.x][tmp2.y]!='#') { vis[tmp2.z][tmp2.x][tmp2.y]=1; q.push(tmp2); } } } if(!flag) printf("Trapped!\n"); } }
本文介绍了一个解决三维地牢迷宫问题的算法,通过广度优先搜索找到从起点到出口的最短路径,详细展示了如何遍历地图并记录已访问的位置。
470

被折叠的 条评论
为什么被折叠?



