Dungeon Master[地牢大师]

Dungeon Master
👆题目链接

BFS【三维】

AC代码

#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

struct w
{
    int x;
    int y;
    int z;
};
typedef struct w whi;
const int N = 35;
char g[N][N][N];
int d[N][N][N];
int l, r, c;
int dx[6] = {1, -1, 0, 0, 0, 0};
int dy[6] = {0, 0, -1, 0, 1, 0};
int dz[6] = {0, 0, 0, 1, 0, -1};
int s1, s2, s3, d1, d2, d3;
int bfs()
{
    queue<whi> q;
    memset(d, -1, sizeof(d));
    d[s1][s2][s3] = 0;
    whi f;
    f.x = s1;
    f.y = s2;
    f.z = s3;
//    q.push({s1, s2, s3});
    q.push(f);
    while(q.size())
    {
        whi t = q.front();
        q.pop();
        
        for(int i = 0; i < 6; ++i)
        {
            
            int x = t.x+dx[i];
            int y = t.y+dy[i];
            int z = t.z+dz[i];
//            cout<<"i:"<<i<<","<<x<<" "<<y<<" "<<z<<endl;
//            if(x >= 0 && x < l && y >= 0 && y < r && z >= 0 && z < c )
//                cout<<g[x][y][z]<<endl;
            if(x >= 0 && x < l && y >= 0 && y < r && z >= 0 && z < c && g[x][y][z] == '.' && d[x][y][z] == -1)
            {
//                cout<<"i:"<<i<<","<<x<<" "<<y<<" "<<z<<endl;
                whi m;
                m.x = x;
                m.y = y;
                m.z = z;
                d[x][y][z] = d[t.x][t.y][t.z]+1;
                q.push(m);
            }
        }
    }
    return d[d1][d2][d3];
}

int main()
{
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    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')
                    {
                        s1 = i;
                        s2 = j;
                        s3 = k;
                    }
                    if(g[i][j][k] == 'E')
                    {
                        d1 = i;
                        d2 = j;
                        d3 = k;
                        g[i][j][k] = '.';
                    }
                }
            }
            getchar();
        }
//        cout<<s1<<" "<<s2<<" "<<s3<<endl;
//        cout<<d1<<" "<<d2<<" "<<d3<<endl;
        
        
//        for(int i = 0; i < l; ++i)
//        {
//            for(int j = 0; j < r; ++j)
//            {
//                for(int k = 0; k < c; ++k)
//                {
//                    cout<<g[i][j][k];
//                }
//                cout<<endl;
//            }
//            cout<<endl;
//        }
//        cout<<bfs()<<endl;
        int ans = bfs();
        if(ans == -1)
        {
            cout<<"Trapped!"<<endl;
        }
        else
        {
            cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
        }
//        for(int i = 0; i < l; ++i)
//        {
//            for(int j = 0; j < r; ++j)
//            {
//                for(int k = 0; k < c; ++k)
//                {
//                    printf("%4d", d[i][j][k]);
//                }
//                cout<<endl;
//            }
//            cout<<endl;
//        }
    }
    return 0;
}

BFS【二维】

走迷宫(自行百度)

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ocodotial

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值