题意
走迷宫,可以跳跃走到下一层,从S到E每走一步花费一分钟,问你如果能到终点E,需要多少分钟,或者直接说明不能到达终点E
思路
除了在平面x0y上下左右行走之外,还可在z轴的方向上行走,这样只需要把那个控制方向的数组开成三维的就可以了,这里注意一点的是,三维数组的第一个是z,其他的地方和二维的类似,就没什么难点了。
code
#include<iostream>
#include<queue>
#include<cstring>
#define endl '\n'
using namespace std;
const int maxn=33;
int t,n,m;
struct node{
int x,y,z;
int step;
}ss,ee,now,nex;
bool vis[maxn][maxn][maxn];
char mp[maxn][maxn][maxn];
int dx[]={0,0,1,0,-1,0};
int dy[]={0,0,0,1,0,-1};
int dz[]={-1,1,0,0,0,0};
bool check(int x,int y,int z){
if(vis[z][x][y]) return false;
if(mp[z][x][y]=='#') return false;
if(x<1||x>n||y<1||y>m||z<1||z>t) return false;
return true;
}
void bfs(){
vis[ss.z][ss.x][ss.y]=true;
queue<node>q;
q.push(ss);
while(!q.empty()){
now=q.front();
q.pop();
if(now.x==ee.x&&now.y==ee.y&&now.z==ee.z){
cout<<"Escaped in "<<now.step<<" minute(s)."<<endl;
return;
}
for(int i=0;i<6;i++){
nex.x=now.x+dx[i];
nex.y=now.y+dy[i];
nex.z=now.z+dz[i];
if(check(nex.x,nex.y,nex.z)){
//if(nex.z>=1&&nex.z<=t&&nex.x>=1&&nex.x<=n&&nex.y>=1&&nex.y<=m&&!vis[nex.z][nex.x][nex.y]&&mp[nex.z][nex.x][nex.y]!='#'){
//cout<<nex.step<<endl;
nex.step=now.step+1;
vis[nex.z][nex.x][nex.y]=true;
q.push(nex);
}
}
}
cout<<"Trapped!"<<endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>t>>n>>m&&t&&n&&m){
for(int i=1;i<=t;i++){
for(int j=1;j<=n;j++){
cin>>(mp[i][j]+1);
for(int k=1;k<=m;k++){
if(mp[i][j][k]=='S')
ss.z=i,ss.x=j,ss.y=k,ss.step=0;
else if(mp[i][j][k]=='E')
ee.z=i,ee.x=j,ee.y=k,ee.step=0;
}
}
}
memset(vis,false,sizeof(vis));
bfs();
}
return 0;
}