3D迷宫,找从开始点到终点的最短路,挺水的一题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
struct Point3D
{
int x;
int y;
int z;
};
int x,y,z;
Point3D s,e;
queue<Point3D> q;
char dun[40][40][40];
int mem[40][40][40];
const int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
bool judge(Point3D t)
{
if (t.x < 0 || t.x >= x || t.y < 0 || t.y >= y || t.z < 0 || t.z >= z)
return false;
if (dun[t.x][t.y][t.z] == '#')
return false;
return true;
}
void BFS()
{
int i;
Point3D tp,ttp;
while (!q.empty())
q.pop();
q.push(s);
memset(mem,0X3f,sizeof(mem));
mem[s.x][s.y][s.z]=0;
while (!q.empty())
{
tp=q.front();
q.pop();
for (i=0; i<6; i++)
{
ttp.x=tp.x+dir[i][0];
ttp.y=tp.y+dir[i][1];
ttp.z=tp.z+dir[i][2];
if (judge(ttp))
{
if (mem[ttp.x][ttp.y][ttp.z] > mem[tp.x][tp.y][tp.z]+1)
{
mem[ttp.x][ttp.y][ttp.z]=mem[tp.x][tp.y][tp.z]+1;
q.push(ttp);
}
}
}
}
}
int main()
{
int t,i,j,k;
char tc;
while (1)
{
scanf("%d%d%d",&x,&y,&z);
if (x == 0 && y == 0 && z == 0)
break;
getchar();
for (i=0; i<x; i++)
{
for (j=0; j<y; j++)
{
for (k=0; k<z; k++)
{
scanf("%c",&dun[i][j][k]);
if (dun[i][j][k] == 'S')
{
s.x=i;
s.y=j;
s.z=k;
}
else if (dun[i][j][k] == 'E')
{
e.x=i;
e.y=j;
e.z=k;
}
}
getchar();
}
getchar();
}
BFS();
if (mem[e.x][e.y][e.z] == INF)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",mem[e.x][e.y][e.z]);
}
return 0;
}