题意:一个三维的地牢 'S' 表示其实, ‘E' 表示出口。’.' 表示可行, ‘#' 表示不可行。
题解:
#include <queue>
#include <iostream>
using namespace std;
char maze[31][31][31];
bool check[31][31][31];
int dir[6][3] = { {1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1} };
int l, r, c, si, sj, sk, ei, ej, ek;
struct cube
{
int x, y, z, step;
};
bool isLeagal ( int x, int y, int z )
{
if ( maze[x][y][z] == '#' || check[x][y][z] || x < 0 || x >= l || y < 0 || y >= r || z < 0 || z >= c )
return false;
return true;
}
int bfs ()
{
queue<cube> Q;
cube point,cur,next;
point.x = si;
point.y = sj;
point.z = sk;
point.step = 0;
check[si][sj][sk] = 1;
Q.push(point);
int a, b, c, i;
while ( ! Q.empty() )
{
cur = Q.front();
Q.pop();
if ( cur.x == ei && cur.y == ej && cur.z == ek )
return cur.step;
for ( i = 0; i < 6; i++ )
{
a = cur.x + dir[i][0];
b = cur.y + dir[i][1];
c = cur.z + dir[i][2];
if ( isLeagal(a,b,c) )
{
next.x = a;
next.y = b;
next.z = c;
next.step = cur.step + 1;
check[a][b][c] = 1;
Q.push(next);
}
}
}
return 0;
}
int main()
{
while ( cin >> l >> r >> c && l && r && c )
{
for ( int i = 0; i < l; i++ )
for ( int j = 0; j < r; j++ )
{
cin >> maze[i][j];
for ( int k = 0; k < c; k++ )
{
if ( maze[i][j][k] == 'S' )
{
si = i; sj = j; sk = k;
}
if ( maze[i][j][k] == 'E' )
{
ei = i; ej = j; ek = k;
}
}
}
memset(check,0,sizeof(check));
int res = bfs ();
if ( res != 0 )
cout << "Escaped in " << res << " minute(s)." << endl;
else
cout << "Trapped!" << endl;
}
return 0;
}