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 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!
bfs, 把二维转换成三维就可以了
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int l, h,w, cnt;
char Map[31][31][31];
int tx[] = {0,0,1,-1,0,0};
int ty[] = {0,0,0,0,1,-1};
int tz[] = {1,-1,0,0,0,0};
bool vis[31][31][31];
bool check(int x, int y, int z)
{
if(x>=0&&x<l&&y>=0&&y<h&&z>=0&&z<w&&!vis[x][y][z]&&Map[x][y][z]!='#')
return true;
return false;
}
struct node
{
int x,y,z,s;
} p,ww;
int bfs(int x, int y, int z,int s)
{
p.x = x,p.y = y,p.z = z,p.s = s;
queue<node>qq;
qq.push(p);
while(!qq.empty())
{
p = qq.front();
qq.pop();
if(Map[p.x][p.y][p.z]=='E')
{
return p.s;
}
for(int i = 0; i<6; ++i)
{
int xx = tx[i]+p.x;
int yy = ty[i]+p.y;
int zz = tz[i]+p.z;
if(check(xx,yy,zz))
{
vis[xx][yy][zz] = 1;
ww.x = xx;
ww.y = yy;
ww.z = zz;
ww.s = p.s+1;
qq.push(ww);
}
}
}
return -1;
}
int main()
{
int x, y,z;
while(~scanf("%d%d%d",&l,&h,&w)&&(h||w||l))
{
x = -1;
for(int i = 0; i<l; ++i)
{
gets(Map[i][0]);
for(int j = 0; j<h; ++j)
{
gets(Map[i][j]);
if(x==-1)
{
for(int k = 0; k<w; ++k)
{
if(Map[i][j][k]=='S')
{
x = i;
y = j;
z = k;
break;
}
}
}
}
}
memset(vis,0,sizeof(vis));
vis[x][y][z] = 1;
cnt = bfs(x,y,z, 0);
if(-1==cnt)printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n",cnt);
}
return 0;
}