地牢大师(三维bfs)

原题链接

解题思路:本题就是一个简单的三维bfs题,使用PIII或者结构体储存好坐标点,然后定义好偏移量,接下来就可以直接按照bfs的套路写即可
C++代码
#include <bits/stdc++.h>
// #define int long long 
#define x first
#define y second
using namespace std;
const int N = 110;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
char g[N][N][N];
int dist[N][N][N];
int l, r, c;
PIII ss, ee;
int dx[6] = {1, -1, 0, 0, 0, 0};
int dy[6] = {0, 0, 1, -1, 0, 0};
int dz[6] = {0, 0, 0, 0, 1, -1};

void bfs()
{
    memset(dist, 0x3f, sizeof dist);
    queue<PIII> q;
    q.push(ss);
    dist[ss.x.x][ss.x.y][ss.y] = 0;
    while (q.size())
    {
        auto t = q.front();
        q.pop();
        int xx = t.x.x, yy = t.x.y, zz = t.y;
        for (int i = 0; i < 6; i ++ )
        {
            int aa = xx + dx[i], bb = yy + dy[i], cc = zz + dz[i];
            if (aa >= 0 && aa < l && bb >= 0 && bb < r && cc >= 0 && cc < c && dist[aa][bb][cc] == 0x3f3f3f3f && g[aa][bb][cc] != '#')
            {
                dist[aa][bb][cc] = dist[xx][yy][zz] + 1;
                q.push({{aa, bb}, cc});
            }
        }
    }
}

signed main()
{
    while (cin >> l >> r >> c, l || r || c)
    {
        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') ss = {{i, j}, k};
                    if (g[i][j][k] == 'E') ee = {{i, j}, k};
                }
            }
        }
        bfs();
        if (dist[ee.x.x][ee.x.y][ee.y] != 0x3f3f3f3f) cout << "Escaped in " << dist[ee.x.x][ee.x.y][ee.y] <<  " minute(s)." <<endl;
        else cout << "Trapped!" << endl;
    }
    return 0;
}

创作不易,大家可以在AcWing注册账号,参与一下AC之星计划,我的邀请码是MYRGS。对题解有任何疑问可以在评论区下面评论在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值