Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6095 | Accepted: 2424 |
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
#include<iostream>
using namespace std;
char map[31][31][31];
struct
{
int x,y,z,step;
}a[30*30*31];
bool flag;
int i,j,k,cnt,beg,end,l,r,c;
int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
bool ok(int x,int y,int z)
{
if(x>0&&x<=r&&y>0&&y<=c&&z>0&&z<=l&&map[z][x][y]=='.') return true;
if(map[z][x][y]=='E') return true;
return false;
}
int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
while(scanf("%d%d%d",&l,&r,&c))
{
int si,sj,sk;
if(l==0&&r==0&&c==0) break;
for(i=1;i<=l;i++)
for(j=1;j<=r;j++)
for(k=1;k<=c;k++)
{
cin>>map[i][j][k];
if(map[i][j][k]=='S')
si=i,sj=j,sk=k;
}
a[0].x=sj,a[0].y=sk,a[0].z=si,a[0].step=0;
cnt=1;
beg=0;end=1;
flag=false;
while(!flag&&beg<end)
{
for(i=beg;i<end&&!flag;i++)
{
for(j=0;j<6&&!flag;j++)
if(ok(a[i].x+dir[j][0],a[i].y+dir[j][1],a[i].z+dir[j][2]))
{
if(map[a[i].z+dir[j][2]][a[i].x+dir[j][0]][a[i].y+dir[j][1]]=='E')
{
printf("Escaped in %d minute(s)./n",a[i].step+1);
flag=true;
}
a[cnt].x=a[i].x+dir[j][0];
a[cnt].y=a[i].y+dir[j][1];
a[cnt].z=a[i].z+dir[j][2];
a[cnt++].step=a[i].step+1;
map[a[i].z+dir[j][2]][a[i].x+dir[j][0]][a[i].y+dir[j][1]]='#';
}
}
beg=end;
end=cnt;
}
if(!flag) printf("Trapped!/n");
}
return 0;
}