这道题被自己坑了好久,不知道为啥用一个int型指针变量接收int指针函数就不对了,一直re,所以后来改成了一个结构体变量来存x,y,z。唉,真心不懂为啥那样就不对。
#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
char Map[50][50][50];
int dir[6][3]= {{0,0,1},{0,0,-1},{-1,0,0},{0,1,0},{1,0,0},{0,-1,0}};
struct node
{
int x,y,z;
int step;
};
struct node a;
void init()
{
for(int i=0; i<35; i++)
for(int j=0; j<35; j++)
for(int k=0; k<35; k++)
Map[i][j][k]='#';
}
void input(int x,int y,int z)
{
for(int i=z; i>=1; i--)
{
getchar();
for(int j=1; j<=x; j++)
{
for(int k=1; k<=y; k++)
{
scanf("%c",&Map[j][k][i]);
if(Map[j][k][i]=='S')
{
a.x=j;
a.y=k;
a.z=i;
}
}
getchar();
}
}
}
int bfs()
{
queue<node>q;
struct node b;
a.step=0;
q.push(a);
while(!q.empty())
{
a=q.front();
for(int i=0; i<6; i++)
{
b.x=a.x+dir[i][0];
b.y=a.y+dir[i][1];
b.z=a.z+dir[i][2];
if(Map[b.x][b.y][b.z]=='.')
{
b.step=a.step+1;
Map[b.x][b.y][b.z]='#';
q.push(b);
}
else if(Map[b.x][b.y][b.z]=='E')
{
return 1+a.step;
}
}
q.pop();
}
return -1;
}
int main()
{
int z,x,y;
while(scanf("%d%d%d",&z,&x,&y)!=EOF&&z!=0&&x!=0&&y!=0)
{
init();
input(x,y,z);
int ans=bfs();
if(ans>0)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
return 0;
}