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!
题意:从一个点到另一个点,若能到达则输出最低步数,若不能到达则输出Trapped!
思路:本题为BFS题,从所给点开始向前后左右上下六个方向搜索,直到达到要求的点或直到无路可走。
3.代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
int dx[6] = {0,0,0,0,1,-1};
int dy[6] = {0,0,1,-1,0,0};
int dz[6] = {1,-1,0,0,0,0};
int visit[40][40][40], map[40][40][40];
struct node
{
int x, y, z, step;
}st, ed;
queue<node> q;
bool judge(int x, int y, int z)
{
if(!map[x][y][z] || visit[x][y][z])
return false;
return true;
}
int BFS()
{
int x, y, z, t, i;
while(!q.empty())
{
node tmp = q.front();
q.pop();
x = tmp.x;
y = tmp.y;
z = tmp.z;
t = tmp.step;
for(i = 0; i < 6; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
int nz = z + dz[i];
if(judge(nx,ny,nz))
{
if(nx == ed.x && ny == ed.y && nz == ed.z)
return t + 1;
visit[nx][ny][nz] = 1;
node temp;
temp.x = nx;
temp.y = ny;
temp.z = nz;
temp.step = t + 1;
q.push(temp);
}
}
}
return -1;
}
int main()
{
int L, R, C, i, j, k;
char ch;
while(~scanf("%d%d%d",&L, &R, &C) && (L + R + C))
{
while(!q.empty()) q.pop();
memset(visit,0,sizeof(visit));
memset(map,0,sizeof(map));
for(i = 1; i <= L; i++)
for(j = 1; j <= R; j++)
for(k = 1; k <= C; k++)
{
cin >> ch;
if(ch == '.')
map[j][k][i] = 1;
else if(ch == 'S')
{
st.x = j;
st.y = k;
st.z = i;
st.step = 0;
visit[j][k][i] = 1;
map[j][k][i] = 1;
q.push(st);
}
else if(ch == 'E')
{
ed.x = j;
ed.y = k;
ed.z = i;
map[j][k][i] = 1;
}
}
int ans = BFS();
if(ans == -1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}