三维广搜,又学到一个读入换行的方式 AcWing 1096 地牢大师
#include<bits/stdc++.h>
using namespace std;
const int N = 110, INF = 1e9;
struct Node{
int x, y, z;
}an[N];
char g[N][N][N];
int l, r, c;
int dis[N][N][N];
bool st[N][N][N];
queue<Node>q;
int dx[6] = {0, 0, 1, -1, 0, 0};
int dy[6] = {1, -1, 0, 0, 0, 0};
int dz[6] = {0, 0, 0, 0, 1, -1};
bool flag;
int ans;
int main()
{
while(1){
cin>>l>>r>>c;
if(l == 0 && r == 0 && c == 0){
break;
}
memset(dis, 0, sizeof(dis));
flag = false;
memset(st, 0, sizeof st);
ans = INF;
for(int i = 1; i <= l; i ++ ){
for(int j = 1; j <= r; j ++ ){
string str;
cin>>str;
for(int k = 1; k <= c; k ++ ){
g[i][j][k] = str[k - 1];
if(g[i][j][k] == 'S'){
q.push({i, j, k});
st[i][j][k] = true;
}
}
}
}
while(!q.empty()){
auto op = q.front();
q.pop();
int x1 = op.x, y1 = op.y, z1 = op.z;
for(int i = 0; i < 6; i ++ ){
int xx = x1 + dx[i], yy = y1 + dy[i], zz = z1 + dz[i];
if(!st[xx][yy][zz] && xx >= 1 && xx <= l && yy >= 1 && yy <= r && zz >= 1 && zz <= c && g[xx][yy][zz] != '#'){
q.push({xx, yy, zz});
st[xx][yy][zz] = true;
dis[xx][yy][zz] = dis[x1][y1][z1] + 1;
if(g[xx][yy][zz] == 'E'){
flag = true;
ans = min(ans, dis[xx][yy][zz]);
}
}
}
}
if(flag) cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
else cout<<"Trapped!"<<endl;
}
return 0;
}
|