#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <iomanip>
#include <cmath>
#include <set>
#include <map>
#include <queue>
using namespace std;
int row,col,heit;
int g[40][40][40];
int dia[6][3]={0,1,0,0,-1,0,0,0,-1,0,0,1,-1,0,0,1,0,0};
int vis[40][40][40];
struct node{
int r,c,h;
};
node s,e;
node md(int _h,int _r,int _c){
node Node;
Node.c=_c;
Node.h=_h;
Node.r=_r;
return Node;
}
void init(){
memset(vis,-1,sizeof vis);
}
void bfs(){
queue<node>q;
while (!q.empty())q.pop();
q.push(s);
vis[s.h][s.r][s.c] = 0;
while (!q.empty()){
node temp = q.front();
q.pop();
// cout << "lalalademaxiya!!"<<temp.h <<" "<<temp.r <<" "<<temp.c<<endl;
for (int i = 0;i<6;++i){
//int dia[6][3]={0,1,0,0,-1,0,0,0,-1,0,0,1,-1,0,0,1,0,0};
int dh = temp.h + dia[i][0],dr = temp.r + dia[i][1],dc = temp.c + dia[i][2];
// cout << dh <<" "<< dr <<" "<< dc <<" "<< endl;
if (dh <= 0 || dr <=0 || dc <=0 || dr > row || dh >heit || dc > col )continue;
if (g[dh][dr][dc]==1 && vis[dh][dr][dc]==-1){
vis[dh][dr][dc] = vis[temp.h][temp.r][temp.c] + 1;
q.push(md(dh,dr,dc));
// cout << dh <<" "<<dr<<" "<<dc<<endl;
}
}
}
}
int main(){
// freopen("t.x","w",stdout);
while (cin >> heit >> row >>col ){
if (!row&&!col&&!heit){
return 0;
}
for (int i = 1;i<=heit;++i){
for (int j=1;j<=row;++j){
for (int k=1;k<=col;++k){
char c;
cin >> c;
if (c == '\n'||c=='\t')cin >>c >> c;
if (c=='#')g[i][j][k] = 0;
if (c=='.')g[i][j][k] = 1;
if (c=='S'){
s = md(i,j,k);
g[i][j][k] = 1;
}
if (c=='E'){
e=md(i,j,k);
g[i][j][k] = 1;
}
}
}
}
init();
bfs();
if(vis[e.h][e.r][e.c]>-1) cout << "Escaped in "<<vis[e.h][e.r][e.c]<<" minute(s).\n";else cout<<"Trapped!\n";
}
return 0;
}
0223B
最新推荐文章于 2022-02-28 10:36:50 发布