题解
2024-06-16 21:55:34
发布于:广东
2阅读
0回复
0点赞
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct node{
int x, y, z, step;
};
char mp[105][105][105];
bool vis[105][105][105];
int dir[6][3] = {-1, 0, 0, 0, -1, 0, 0, 0, -1, 1, 0, 0, 0, 1, 0, 0, 0, 1};
int n, m, k, x, y, z;
bool check(int x, int y, int z){
if(x < 1 || x > n) return 0;
if(y < 1 || y > m) return 0;
if(z < 1 || z > k) return 0;
if(mp[x][y][z] == '#' || vis[x][y][z]) return 0;
return 1;
}
int bfs(){
queue <node> q;
q.push({x, y, z, 0});
vis[x][y][z] = 1;
while(!q.empty()){
node head = q.front();
q.pop();
if(mp[head.x][head.y][head.z] == 'E') return head.step;
for(int i = 0; i < 6; i++){
int xx = head.x + dir[i][0], yy = head.y + dir[i][1], zz = head.z + dir[i][2];
if(check(xx, yy, zz)){
vis[xx][yy][zz] = 1;
q.push({xx, yy, zz, head.step + 1});
}
}
}
return -1;
}
int main(){
scanf("%d%d%d", &n, &m, &k);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
scanf("%s", mp[i][j] + 1);
for(int l = 1; l <= k; l++){
if(mp[i][j][l] == 'S'){
x = i, y = j, z = l;
}
}
}
}
int ans = bfs();
if(ans == -1) cout << "Trapped!";
else printf("Escaped in %d minute(s).", ans);
return 0;
}
这里空空如也
有帮助,赞一个