题解
2024-02-05 19:19:37
发布于:浙江
39阅读
0回复
0点赞
其实这道题很简单 : )
代码:
#include <bits/stdc++.h>
using namespace std;
int n, m, u, ans = -1, a_n, a_m, a_u, ans_n, ans_m, ans_u;
char a[110][110][110];
bool vis[110][110][110];
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 x, y, z, step;
};
bool check(int x, int y, int z){
return z >= 1 && z <= u && x >= 1 && y >= 1 && x <= n && y <= m && a[x][y][z] != '#' && vis[x][y][z] == false;
}
void bfs(int x, int y, int z, int step){
queue<Node> q;
q.push({x, y, z, step});
vis[x][y][z] == true;
while(!q.empty()){
Node t = q.front();
if(t.x == ans_n && t.y == ans_m && t.z == ans_u){
ans = t.step;
return ;
}
q.pop();
for(int i = 0;i <= 5;i++){
int nx = t.x + dir[i][0];
int ny = t.y + dir[i][1];
int nz = t.z + dir[i][2];
if(check(nx, ny, nz)){
q.push({nx, ny, nz, t.step + 1});
vis[nx][ny][nz] = true;
}
}
}
}
int main(){
cin >> n >> m >> u;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
for(int k = 1;k <= u;k++){
cin >> a[i][j][k];
if(a[i][j][k] == 'E'){
ans_n = i;
ans_m = j;
ans_u = k;
}
if(a[i][j][k] == 'S'){
a_n = i;
a_m = j;
a_u = k;
}
}
}
}
bfs(a_n, a_m, a_u, 0);
if(ans == -1) cout << "Trapped!";
else cout << "Escaped in " << ans << " minute(s).";
return 0;
}
全部评论 1
《很简单》
2024-07-11 来自 云南
0
有帮助,赞一个