- 问答
- 2016-08-23 11:28:59 @
描述
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?
输入
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.
输出
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!
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
struct node{
int x,y,z,step;
};
node st,en;
int map[35][35][35];
bool vis[35][35][35];
const int dy[]={1,0,-1,0},
dz[]={0,1,0,-1};
int L,R,C;
void read()
{
memset(map,0,sizeof(map));
memset(vis,0,sizeof(vis));
for(int i=1;i<=L;i++)
for(int j=1;j<=R;j++)
{
char c[35];
scanf("%s",c);
for(int k=0;k<C;k++)
{
map[i][j][k+1]=c[k];
if(c[k]=='S')
{
st.x=i;st.y=j;st.z=k+1;
}
if(c[k]=='E')
{
en.x==i;en.y=j;en.z=k+1;
}
}
}
}
void bfs(int x,int y,int z)
{
int flag=1;
vector<node> q;
node temp;
temp.x=x,temp.y=y,temp.z=z,temp.step=0;
q.push_back(temp);
vis[x][y][z]=true;
while(flag)
{
flag=0;
for(int k=0;k<q.size();k++)
{
node sta=q[k];
for(int i=0;i<4;i++)
{
int tx=sta.x;
int ty=sta.y+dy[i];
int tz=sta.z+dz[i];
int ts=sta.step+1;
if(ty>R)
{
if(tx+1<=L)
{
tx+=1;
ty=sta.y;
}
else continue;
}
if(map[tx][ty][tz]=='E')
{
printf("Escaped in %d minute(s).\n",ts);
return;
}
if(map[tx][ty][tz]=='#') continue;
if(map[tx][ty][tz]=='.'&&!vis[tx][ty][tz])
{
node tp;
tp.x=tx;tp.y=ty;tp.z=tz;tp.step=ts;
q.push_back(tp);
flag=1;
vis[tx][ty][tz]=1;
continue;
}
}
}
}
printf("Trapped!\n");
}
int main()
{
while(scanf("%d%d%d",&L,&R,&C)==3&&L&&R&&C)
{
read();
bfs(st.x,st.y,st.z);
}
return 0;
}
本题的样例过了,就是WA,求大牛指点