校OJ P1139 -- 秦心的面具

博客介绍了如何解决一道名为'秦心的面具'的01迷宫问题,提供了详细的解题思路和代码实现,适合编程初学者和爱好者学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一道简单的01迷宫题,直接发代码

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

const int maxn = 52;
const int dx[6] = {0,0,0,0,1,-1};
const int dy[6] = {0,0,-1,1,0,0};
const int dz[6] = {1,-1,0,0,0,0};
struct Point{
    int x, y, z, s;
    Point( int a, int b, int c, int d ) : x(a), y(b), z(c), s(d) {};
    bool operator < ( const Point a ) const { return s > a.s; }
    bool operator > ( const Point a ) const { return s < a.s; }
};
bool maps[maxn][maxn][maxn], vis[maxn][maxn][maxn], isget;
int sx, sy, sz, tim;
priority_queue<Point> que;

void init() {
    isget = false;
    memset(maps, 0, sizeof(maps));
    memset(vis, 0, sizeof(vis));
    while( !que.empty() ) que.pop();
}

void input() {
    scanf("%d %d %d %d", &sz, &sx, &sy, &tim);
    for( int i = 1; i <= sz; i++ )
        for( int j = 1; j <= sx; j++ )
            for( int k = 1; k <= sy; k++ )
                scanf("%d", &maps[j][k][i]);
}

void solve() {
    //BFS;
    vis[1][1][1] = 1; //起点标记一下
    que.push( Point( 1,1,1,0 ) );
    while(!que.empty() && !isget) {
        Point cur = que.top(); que.pop();
        for( int i = 0; i < 6; i++ ) {
            int tx = cur.x + dx[i], ty = cur.y + dy[i], tz = cur.z + dz[i];
            if( tx < 1 || ty < 1 || tz < 1 || tx > sz || ty > sy || tz > sz ) continue;
            if( vis[tx][ty][tz] || maps[tx][ty][tz] ) continue;
            if( tx == sx && ty == sy && tz == sz ) {
                isget = true;
                if( cur.s+1 > tim ) break;
                printf("%d\n", cur.s + 1);
                return;
            }
            if( cur.s+1 > tim ) isget = true;
            vis[tx][ty][tz] = 1;
            que.push( Point(tx, ty, tz, cur.s+1) );
        }
    }
    printf("-1\n");
}

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        init();
        input();
        solve();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值