Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 28301 | Accepted: 11055 |
Description
Is an escape possible? If yes, how long will it take?
Input
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
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!
Source
C++比G++省了一半的时间内存
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
char a[35][35][35];
int L,R,C,flag;
int sx,sy,sz;
int vist[35][35][35];
int xx[6]={1,-1,0,0,0,0};
int yy[6]={0,0,1,-1,0,0};
int zz[6]={0,0,0,0,1,-1};
struct node
{
int Z,X,Y;
int step;
}now,next;
int BFS(int z,int x,int y)
{
int i;
vist[z][x][y] = 1;
queue<node>Q;
now.step = 0;
now.X = x;
now.Y = y;
now.Z = z;
Q.push(now);
while(!Q.empty())
{
now = Q.front();
Q.pop();
if(a[now.Z][now.X][now.Y] == 'E')
{
printf("Escaped in %d minute(s).\n",now.step);
return 1;
}
for(i = 0; i < 6; i++)
{
next.X = now.X+xx[i];
next.Y = now.Y+yy[i];
next.Z = now.Z+zz[i];
next.step = now.step+1;
if(!vist[next.Z][next.X][next.Y] && a[next.Z][next.X][next.Y]!='#' && next.Z>=0 && next.Z<L &&next.X>=0 && next.X<R && next.Y>=0 && next.Y<C)
{
vist[next.Z][next.X][next.Y] = 1;
Q.push(next);
}
}
}
return 0;
}
int main()
{
while(~scanf("%d %d %d%*c",&L,&R,&C) && (L||R||C))
{
for(int i = 0; i < L; i++)
for(int j = 0; j < R; j++)
{
cin>>a[i][j];
for(int l = 0; l < C; l++)
{
if(a[i][j][l] == 'S')
{
sz = i;
sx = j;
sy = l;
}
}
}
memset(vist,0,sizeof(vist));
if(BFS(sz,sx,sy)==0)
printf("Trapped!\n");
}
return 0;
}