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?
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.
Output
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!
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 <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
struct node
{
int z,x,y;
int step;
node(){};
node(int z1,int x1,int y1,int step1):z(z1),x(x1),y(y1),step(step1){}
};
int l,n,m,z1,x1,y1,z2,x2,y2,minn;
bool vis[50][50][50];
bool flag;
char mp[50][50][50];
int dir[6][3]={
{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
node s,e;
void bfs(node s,node e)
{
flag=0;
queue<node>q;
q.push(node(s.z,s.x,s.y,s.step));
while(!q.empty())
{
node a=q.front();
q.pop();
for(int i=0;i<6;i++)
{
int zz=a.z+dir[i][0];
int xx=a.x+dir[i][1];
int yy=a.y+dir[i][2];
if(zz>=1&&zz<=l&&xx>=1&&xx<=n&&yy>=1&&yy<=m&&!vis[zz][xx][yy]&&mp[zz][xx][yy]=='.')
{
q.push(node(zz,xx,yy,(a.step+1)));
vis[zz][xx][yy]=1;
if(zz==e.z&&xx==e.x&&yy==e.y)
{
minn=a.step+1;
flag=1;
return ;
}
}
}
}
}
int main()
{
while(~scanf("%d%d%d",&l,&n,&m)&&l+n+m)
{
memset(vis,0,sizeof(vis));
getchar();
for(int i=1;i<=l;i++)
{
for(int j=1;j<=n;j++)
{
for(int k=1;k<=m;k++)
{
scanf("%c",&mp[i][j][k]);
if(mp[i][j][k]=='S')
{
s.z=i;
s.x=j;
s.y=k;
}
if(mp[i][j][k]=='E')
{
e.z=i;
e.x=j;
e.y=k;
mp[i][j][k]='.';
}
}
getchar();
}
getchar();
}
bfs(s,e);
if(flag)
cout<<"Escaped in "<<minn<<" minute(s)."<<'\n';
else
cout<<"Trapped!"<<'\n';
}
return 0;
}
dfs:(过不了 超时)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int N=5e6+30;
int dri[6][3]= {
{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
bool vis[50][50][50];
char mp[50][50][50];
int step[50][50][50];
int n,minn,l,r,c;
bool flag;
int x1,y1,z1,x2,y2,z2;
void dfs(int x,int y,int z,int tmp)
{
if(tmp>=minn||step[x][y][z]<=tmp)
return ;
step[x][y][z]=tmp;
for(int i=0; i<6; i++)
{
int xx=x+dri[i][0];
int yy=y+dri[i][1];
int zz=z+dri[i][2];
if(xx<=l&&xx>=1&&yy<=r&&yy>=1&&zz<=c&&zz>=1&&mp[xx][yy][zz]=='.'&&!vis[xx][yy][zz])
{
vis[xx][yy][zz]=1;
if(xx==x2&&yy==y2&&zz==z2)
{
flag=1;
minn=min(minn,tmp+1);
}
else
dfs(xx,yy,zz,tmp+1);
vis[xx][yy][zz]=0;
}
}
}
int main()
{
while(~scanf("%d%d%d",&l,&r,&c)&&l&&r&&c)
{
memset(vis,0,sizeof(vis));
memset(step,inf,sizeof(step));
getchar();
flag=0;
for(int i=1; i<=l; i++)
{
for(int j=1; j<=r; j++)
{
for(int k=1; k<=c; k++)
{
scanf("%c",&mp[i][j][k]);
if(mp[i][j][k]=='S')
{
x1=i;
y1=j;
z1=k;
}
if(mp[i][j][k]=='E')
{
x2=i;
y2=j;
z2=k;
mp[i][j][k]='.';
}
}
getchar();
}
getchar();
}
minn=inf;
dfs(x1,y1,z1,0);
if(flag)
cout<<"Escaped in "<<minn<<" minute(s)."<<'\n';
else
cout<<"Trapped!"<<'\n';
}
return 0;
}