题意: 从S到E需要多少分钟(只能上下左右前后移动一格,每移动一个耗费1分钟);
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 35;
struct node{
int x, y, z, step;
}now;
char mp[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int 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 bfs(node Start, node End)
{
memset(vis, 0, sizeof(vis));
queue<node> q;
q.push(Start);
while(!q.empty())
{
now = q.front();
q.pop();
if((now.x == End.x) && (now.y == End.y) && (now.z == End.z))
{
return true;
}
node next;
for (int i = 0; i <6; i++)
{
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
next.z = now.z + dir[i][2];
if(next.x >= 0 && next.x < L && next.y >= 0 && next.y < R && next.z >= 0 && next.z < C && !vis[next.x][next.y][next.z] && mp[next.x][next.y][next.z] != '#')
{
next.step = now.step + 1;
vis[next.x][next.y][next.z] = true;
q.push(next);
}
}
}
return false;
}
int main()
{
while(~scanf("%d%d%d", &L, &R, &C)&& (L + R + C))
{
node S, E;
memset(mp, 0, sizeof(mp));
for (int i = 0; i < L; i++)
for(int j = 0; j < R; j++)
for (int k = 0; k < C; k++)
{
cin >> mp[i][j][k];
if(mp[i][j][k] == 'S') S.x = i, S.y = j, S.z = k, S.step = 0;
else if(mp[i][j][k] == 'E') E.x = i, E.y = j, E.z = k;
}
if(bfs(S, E))
cout<<"Escaped in "<<now.step<<" minute(s)."<<endl;
else
cout<<"Trapped!"<<endl;
}
}