刚开始是想只开个map数组,然后入队列的时候map变为‘#’,但是一直WA,暂时未明白,所以另开了个数组,来表示是否已存储。
#include<iostream>
#include<cstdlib>
#include<queue>
#include<string.h>
#include<cstdio>
using namespace std;
int L, R, C, SL, SR, SC, EL, ER, EC;
char t;
char map[33][33][33];
bool pos[33][33][33];
int dir[6][3] = {{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};
struct Node
{
int l, r, c; //position
int step;
char t;
// bool flag;
Node(){
l = r = c = 0;
step = 0;
t = '.';
// flag = false;
}
Node(int q, int b, int d, int w, char m){
l = q, r = b, c = d, step = w, t = m;
}
};
Node tmp1;
void bfs()
{
queue<Node> q;
// Node tmp(SL, SR, SC, 0, 'S');
Node tmp;
tmp.l = SL;
tmp.r = SR;
tmp.c = SC;
tmp.step = 0;
tmp.t = 'S';
// cout << tmp.t << endl;
q.push(tmp);
pos[SL][SR][SC] = true;
while(!q.empty()){
tmp1 = q.front();
if(tmp1.t == 'E'){
return;
}
q.pop();
for(int i=0; i<6; i++){
Node tmp2;
tmp2.l = tmp1.l+dir[i][0];
tmp2.r = tmp1.r+dir[i][1];
tmp2.c = tmp1.c+dir[i][2];
// cout << tmp2.l << " "<<tmp2.r << " " << tmp2.c << " " << map[tmp2.l][tmp2.r][tmp2.c] << endl;
//judge
if((map[tmp2.l][tmp2.r][tmp2.c] == '.'||map[tmp2.l][tmp2.r][tmp2.c] == 'E') && (!pos[tmp2.l][tmp2.r][tmp2.c]) && tmp2.l<=30 && tmp2.r<=30 && tmp2.c<=30 && tmp2.l>0 && tmp2.r>0 && tmp2.c>0){
// tmp2.flag = true;
tmp2.step = tmp1.step+1;
tmp2.t = map[tmp2.l][tmp2.r][tmp2.c];
// cout << tmp2.t << " ";
q.push(tmp2);
pos[tmp2.l][tmp2.r][tmp2.c] = true;
map[tmp2.l][tmp2.r][tmp2.c] = '#';
}
}
// cout << endl;
}
}
int main()
{
// memset(map, '#', sizeof(map));
while(cin >> L >> R >> C && (L && R &&C)){
memset(pos, false, sizeof(pos));
// int n = 0;
for(int i=1; i<=L; i++){
getchar();//层之间的换行符
for(int j=1; j<=R; j++){
for(int k=1; k<=C; k++){
cin >> map[i][j][k];
t = map[i][j][k];
//记录起始位置和结束位置
if(t == 'S'){
SL = i, SR = j, SC = k;
}
if(t == 'E'){
EL = i, ER = j, EC = k;
}
}
getchar();//行之间的换行符
}
}
// cout << SL << SR << SC << endl;
//开始走动
bfs();
if(tmp1.t == 'E'){
printf("Escaped in %d minute(s).\n", tmp1.step);
}
else
printf("Trapped!\n");
}
return 0;
}