[AcWing 1096] 地牢大师
三维迷宫
BFS + 结构体存储
点击查看代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100 + 10;
const int INF = 0x3f3f3f3f;
int L, R, C;
char g[N][N][N];
int d[N][N][N];
int dx[] = {-1, 1, 0, 0, 0, 0};
int dy[] = {0, 0, -1, 1, 0, 0};
int dz[] = {0, 0, 0, 0, -1, 1};
struct Node
{
int x, y, z;
};
void bfs(Node sp, Node ep)
{
memset(d, 0x3f, sizeof d);
queue<Node> q;
q.push(sp);
d[sp.x][sp.y][sp.z] = 0;
while (q.size()) {
auto t = q.front();
q.pop();
for (int i = 0; i < 6; i ++) {
int a = t.x + dx[i];
int b = t.y + dy[i];
int c = t.z + dz[i];
if (a < 0 || a >= L || b < 0 || b >= R || c < 0 || c >= C)
continue;
if (d[a][b][c] != INF || g[a][b][c] == '#')
continue;
d[a][b][c] = d[t.x][t.y][t.z] + 1;
q.push({a, b, c});
}
}
}
void solve()
{
while (cin >> L >> R >> C, L) {
Node sp, ep;
for (int i = 0; i < L; i ++)
for (int j = 0; j < R; j ++)
for (int k = 0; k < C; k ++) {
cin >> g[i][j][k];
if (g[i][j][k] == 'S')
sp = {i, j, k};
if (g[i][j][k] == 'E')
ep = {i, j, k};
}
bfs(sp, ep);
int dist = d[ep.x][ep.y][ep.z];
if (dist == INF)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n", dist);
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
- 用结构体来存点的坐标,上下左右前后六个方向可以移动
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!