HDU1429 胜利大逃亡(续(BFS + 状态压缩)

本文介绍了一个名为“胜利大逃亡”的游戏算法问题,玩家需要帮助角色Ignatius在限定时间内避开魔王并找到出口。文章详细解释了游戏规则、地图元素及状态压缩技巧,并提供了一段使用BFS算法实现的C++代码。

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

胜利大逃亡(续)

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 96 Accepted Submission(s): 61
 
Problem Description
Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……

这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方。刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置。Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一个。魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去。经过若干次的尝试,Ignatius已画出整个地牢的地图。现在请你帮他计算能否再次成功逃亡。只要在魔王下次视察之前走到出口就算离开地牢,如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。
 
Input
每组测试数据的第一行有三个整数n,m,t(2<=n,m<=20,t>0)。接下来的n行m列为地牢的地图,其中包括:

. 代表路
* 代表墙
@ 代表Ignatius的起始位置
^ 代表地牢的出口
A-J 代表带锁的门,对应的钥匙分别为a-j
a-j 代表钥匙,对应的门分别为A-J

每组测试数据之间有一个空行。
 
Output
针对每组测试数据,如果可以成功逃亡,请输出需要多少分钟才能离开,如果不能则输出-1。
 
Sample Input
4 5 17
@A.B.
a*.*.
*..*^
c..b*

4 5 16
@A.B.
a*.*.
*..*^
c..b*
 
Sample Output
16
-1
 
Author
LL
 
Source
ACM暑期集训队练习赛(三)
 
Recommend
linle
 
a~j 10把钥匙 用状态压缩来表示得到哪把钥匙就迎刃而解了。visited[x][y][1 << 11]来表示该点的访问情况。
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <queue>
using namespace std;
int n, m, sss;
constexpr int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int visited[22][22][1 << 11];
char map[22][22];
struct Node {
    int x, y;
    int time, key;
    void set(int xx, int yy, int t = 0, int k = 0) {
        x = xx;
        y = yy;
        time = t;
        key = k;
    }
} start;
bool check(int x, int y) {
    if (x < 0 || x >= n || y < 0 || y >= m)
        return false;
    return true;
}
int BFS() {
    memset(visited, 0, sizeof(visited));
    queue<Node> que;
    Node cur, nex;
    que.push(start);
    visited[start.x][start.y][start.key] = 1;
    while (!que.empty()) {
        cur = que.front();
        que.pop();
        for (int i = 0; i < 4; ++i) {
            nex.set(cur.x + dir[i][0], cur.y + dir[i][1], cur.time, cur.key);
            if (check(nex.x, nex.y) && map[nex.x][nex.y] != '*' && !visited[nex.x][nex.y][nex.key]) {
                if (map[nex.x][nex.y] == '.' || map[nex.x][nex.y] == '@') {
                    visited[nex.x][nex.y][nex.key] = 1;
                    nex.time++;
                    que.push(nex);
                } else if (map[nex.x][nex.y] == '^') {
                    nex.time++;
                    if (nex.time < sss)
                        return nex.time;
                } else if (map[nex.x][nex.y] >= 'a' && map[nex.x][nex.y] <= 'j'&& nex.time < sss) {
                    int k = 1 << (map[nex.x][nex.y] - 'a');
                    if (!visited[nex.x][nex.y][nex.key | k]) {
                        visited[nex.x][nex.y][nex.key | k] = 1;
                        nex.key |= k;
                        nex.time++;
                        que.push(nex);
                    }
                } else if (map[nex.x][nex.y] >= 'A' && map[nex.x][nex.y] <= 'J'&& nex.time < sss) {
                    int k = 1 << (map[nex.x][nex.y] - 'A');
                    if (nex.key & k) {
                        visited[nex.x][nex.y][nex.key] = 1;
                        nex.time++;
                        que.push(nex);
                    }
                }
            }
        }
    }
    return -1;
}
int main() {
    while (cin >> n >> m >> sss) {
        for (int i = 0; i < n; ++i) {
            cin >> map[i];
        }
        
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (map[i][j] == '@') {
                    start.set(i, j);
                }
            }
        }
        cout << BFS() << endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值