http://poj.org/problem?id=2251
三维BFS
注意输入的 map[hang][lie][ceng]
for ceng
for hang
for lie
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<stdio.h>
using namespace std;
int stx, sty, stz, enx, eny, enz;
int l, r, c;
char map[100][100][100];
int vis[100][100][100];
int dx[] = { 1,0,-1,0,0,0 }, dy[] = { 0,1,0,-1,0,0 }, dz[] = { 0,0,0,0,1,-1 };
struct Node
{
int x, y, z, step;
};
queue<Node>q;
void bfs()
{
while (!q.empty())
{
q.pop();
}
q.push({ stx,sty,stz,0 });
vis[stx][sty][stz] = 1;
while (!q.empty())
{
Node p = q.front();
q.pop();
if (p.x == enx && p.y == eny && p.z == enz)
{
printf("Escaped in %d minute(s).\n", p.step);
return;
}
for (int i = 0; i < 6; i++)
{
int nx = p.x + dx[i];
int ny = p.y + dy[i];
int nz = p.z + dz[i];
int ns = p.step + 1;
if (nx < 0 || ny < 0 || nz < 0 || nx >= r || ny >= c || nz >= l || vis[nx][ny][nz]||map[nx][ny][nz]=='#')continue;
else
{
q.push({ nx,ny,nz,ns });
vis[nx][ny][nz] = 1;
}
}
}
cout << "Trapped!" << endl;
}
int main() {
while (cin >> l >> r >> c)
{
memset(vis, 0, sizeof(vis));
if (l == 0 && r == 0 && c == 0)
break;
for (int z = 0; z < l; ++z)//层
{
for (int x = 0; x < r; ++x)//行
{
for (int y = 0; y < c; ++y)//列
{
cin >> map[x][y][z];
if (map[x][y][z] == 'S')
stx = x, sty = y, stz = z;
else if (map[x][y][z] == 'E')
enx = x, eny = y, enz = z;
}
}
}
bfs();
}
}//senak