Description
Input
Output
Escaped in x minute(s).
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
#include<iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 35;
int sx,sy,sz,ex,ey,ez,x,y,z;
char cube[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int dir[6][3] = {1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};
struct Node
{
int x,y,z,t;
Node(int i,int j,int m,int n):x(i),y(j),z(m),t(n){} //构造
Node(){}
}pre;
bool judge(int i, int j, int k) //边界
{
if(i < 0 || i >= x ||j < 0 || j >= y || k < 0 || k >= z)
return false;
return true;
}
void bfs()
{
memset(vis,0,sizeof(vis));
queue <Node> que;
que.push(Node(sx,sy,sz,0));
vis[sx][sy][sz] = 1;
while(!que.empty())
{
pre = que.front(); que.pop();
if(pre.x == ex && pre.y == ey && pre.z == ez)
{
printf("Escaped in %d minute(s).\n", pre.t);
return ;
}
for(int i = 0; i < 6; i++)
{
int xx = pre.x + dir[i][0];
int yy = pre.y + dir[i][1];
int zz = pre.z + dir[i][2];
if(!vis[xx][yy][zz] && judge(xx,yy,zz) && cube[xx][yy][zz] != ‘#‘)
{
vis[xx][yy][zz] = 1;
que.push(Node(xx,yy,zz,pre.t+1));
}
}
}
printf("Trapped!\n");
}
int main()
{
while(scanf("%d %d %d", &x, &y, &z) != EOF)
{
if(!x && !y && !z) break;
for(int i = 0; i < x; i++)
for(int j = 0; j < y; j++)
scanf("%s", cube[i][j]);
for(int i = 0; i < x; i++)
for(int j = 0; j < y; j++)
for(int k = 0; k < z; k++)
if(cube[i][j][k] == ‘S‘)
sx = i,sy = j,sz = k;
else if(cube[i][j][k] == ‘E‘)
ex = i,ey = j,ez = k;
bfs();
}
return 0;
}
原文:http://www.cnblogs.com/KID-XiaoYuan/p/6350319.html