从S出发,E点逃出,只能走.
用队列的方法
三维数组
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define N 110
int dir[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
char a[N][N][N];
int l,r,c;
int x1,y1,z1;
int step[N][N][N];
int f;
struct point
{
int x,y,z,s;
} now,next;
void bfs(int x,int y,int z)
{
now.x = x;
now.y = y;
now.z = z;
now.s = 0;
memset(step,-1,sizeof(step));
step[x][y][z] = 0;
queue<point>Q;
Q.push(now);
while(Q.size())
{
now = Q.front();
Q.pop();
if(now.x==x1&&now.y==y1&&now.z==z1)
{
printf("Escaped in %d minute(s).
",step[now.x][now.y][now.z]);
f = 1;
return ;
}
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];
next.s = now.s + 1;
if(next.x>=0&&next.x<l&&next.y>=0&&next.y<r&&next.z>=0&&next.z<c&&a[next.x][next.y][next.z]!='#'&&step[next.x][next.y][next.z]==-1)
{
a[next.x][next.y][next.z]='#';
step[next.x][next.y][next.z] = next.s;
Q.push(next);
}
}
}
}
int main()
{
int x,y,z;
while(scanf("%d%d%d",&l,&r,&c),l+r+c)
{
f = 0;
for(int i=0; i<l; i++)
{
getchar();
for(int j=0; j<r; j++)
{
for(int k=0; k<c; k++)
{
scanf("%c",&a[i][j][k]);
if(a[i][j][k] == 'S')
{
x = i;
y = j;
z = k;
}
if(a[i][j][k] == 'E')
{
x1 = i;
y1 = j;
z1 = k;
}
}
getchar();
}
}
bfs(x,y,z);
if(!f) printf("Trapped!
");
}
return 0;
}