Dungeon Master
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 Specification
The input file 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 Specification
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!
题意:在一个三维的房间内,问从S走到E至少需要多少步。
思路:简单搜索,没什么可说的。
AC代码如下:
#include<cstdio>
#include<cstring>
using namespace std;
char s[40][40][40];
int t,vis[40][40][40],vc[2][100010][3],ox[6]={-1,0,1,0,0,0},oy[6]={0,1,0,-1,0,0},oz[6]={0,0,0,0,1,-1};
bool flag;
void dfs(int x,int y,int z,int to)
{
int i,j,k;
if(s[z][x][y]=='E')
{
flag=true;
return;
}
for(i=0;i<6;i++)
if(s[z+oz[i]][x+ox[i]][y+oy[i]]!='#' && vis[z+oz[i]][x+ox[i]][y+oy[i]]!=t)
{
vc[to][0][0]++;
vc[to][vc[to][0][0]][0]=x+ox[i];
vc[to][vc[to][0][0]][1]=y+oy[i];
vc[to][vc[to][0][0]][2]=z+oz[i];
vis[z+oz[i]][x+ox[i]][y+oy[i]]=t;
}
}
int main()
{
int i,j,k,l,r,c,step,a,b;
bool ok;
while(scanf("%d%d%d",&l,&r,&c)&& l+r+c)
{
t++;
ok=false;
flag=false;
for(i=1;i<=l;i++)
for(j=1;j<=r;j++)
{
scanf("%s",s[i][j]+1);
if(!ok)
for(k=1;k<=c;k++)
if(s[i][j][k]=='S')
{
ok=true;
vc[0][1][0]=j;
vc[0][1][1]=k;
vc[0][1][2]=i;
vis[i][j][k]=t;
}
}
for(i=0;i<=r+1;i++)
for(j=0;j<=c+1;j++)
s[0][i][j]=s[l+1][i][j]='#';
for(i=1;i<=l;i++)
{
for(j=0;j<=r+1;j++)
s[i][j][0]=s[i][j][c+1]='#';
for(j=0;j<=c+1;j++)
s[i][0][j]=s[i][r+1][j]='#';
}
vc[0][0][0]=1;
step=0;
while(true)
{
if(flag)
break;
step++;
if(step&1)
a=0,b=1;
else
a=1,b=0;
if(vc[a][0][0]==0)
break;
vc[b][0][0]=0;
for(i=1;i<=vc[a][0][0];i++)
dfs(vc[a][i][0],vc[a][i][1],vc[a][i][2],b);
}
if(flag)
printf("Escaped in %d minute(s).\n",step-1);
else
printf("Trapped!\n");
}
}