CSU1855:Shut the Box(二进制状态压缩+BFS)

这篇博客介绍了如何利用二进制状态压缩和BFS算法来解决Shut the Box游戏的策略问题。游戏的目标是在有限的回合内标记尽可能多的数字,每回合可以选择一组未标记的数字,其总和等于指定的值。博客提供了具体的游戏实例,并解释了如何通过二进制状态压缩和BFS找到最优解。
摘要由CSDN通过智能技术生成

题目内容

原题地址http://acm.csu.edu.cn/csuoj/contest/problem?cid=2128&pid=F:

Shut the Box is a one-player game that begins with a set of N pieces labeled from 1 to N. All pieces are initially “unmarked” (in the picture at right, the unmarked pieces are those in an upward position). In the version we consider, a player is allowed up to T turns, with each turn defined by an independently chosen value V (typically determined by rolling one or more dice). During a turn, the player must designate a set of currently unmarked pieces whose numeric labels add precisely to V, and mark them. The game continues either until the player runs out of turns, or until a single turn when it becomes impossible to find a set of unmarked pieces summing to the designated value V (in which case it and all further turns are forfeited). The goal is to mark as many pieces as possible; marking all pieces is known as “shutting the box.” Your goal is to determine the maximum number of pieces that can be marked by a fixed sequence of turns.

As an example, consider a game with 6 pieces and the following sequence of turns: 10, 3, 4, 2. The best outcome for that sequence is to mark a total of four pieces. This can be achieved by using the value 10 to mark the pieces 1+4+5, and then using the value of 3 to mark piece 3. At that point, the game would end as there is no way to precisely use the turn with value 4 (the final turn of value 2 must be forfeited as well). An alternate strategy for achieving the same number of marked pieces would be to use the value 10 to mark four pieces 1+2+3+4, with the game ending on the turn with value 3. But there does not exist any way to mark five or more pieces with that sequence.

Input

Each game begins with a line containing two integers, N, T where 1 ≤ N ≤ 22 represents the number of pieces, and 1 ≤ T ≤ N represents the maximum number of turns that will be allowed. The following line contains T integers designating the sequence of turn values for the game; each such value V will satisify 1 ≤ V ≤ 22. You must read that entire sequence from the input, even though a particular game might end on an unsuccessful turn prior to the end of the sequence. The data set ends with a line containing 0 0.

Output

You should output a single line for each game, as shown below, reporting the ordinal for the game and the maximum number of pieces that can be marked during that game.

题解

二进制状态压缩 + BFS (我也说不出个啥,思路都在代码里面,借鉴了一下队友的QAQ

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>
#include <cstring>

using namespace std;
const int MAX = 1 << 22;
const int MAX_N = 50;
int N, T;
int t[MAX_N], ans;
bool vis[1 << 23];

vector<int> G[MAX_N];
vector<int> M[MAX_N];

struct Node {
    int statu, p, n;
};

// 初始状态,比如n=5 => 111111.....10000
int build(int n) {
    int ans = 1 << n;
    while (!(ans & MAX))
        ans = ans | ans << 1;
    return ans;
}

// 查找所有方案并保存
void init() {
    for (int i = 1; i <= MAX; i++) {
        int sum = 0, t = i, k = 0, num = 0;
        while (t && sum < 23) {
            k++;
            if (t & 1) {
                sum += k;
                num++;
            }
            t >>= 1;
        }
        if (!t) {
            G[sum].push_back(i);
            M[sum].push_back(num);
        }
    }
}

int main() {
    init();
    int kase = 0;
    while (~scanf("%d %d", &N, &T) && N && T) {
        memset(vis, 0, sizeof(vis));
        for (int i = 1; i <= T; i++)
            scanf("%d", t + i);
        queue<Node> Q;
        Node cur;
        Q.push(Node{build(N), 0, 0});
        ans = 0;
        while (!Q.empty()) {
            cur = Q.front();
            Q.pop();
            ans = max(ans, cur.n);
            if (cur.p >= T) continue;
            int p = cur.p + 1, to = t[p], total = (int) G[to].size();
            for (int i = 0; i < total; i++)
                if ((cur.statu & G[to][i]) == 0) {
                    int s = cur.statu | G[to][i];
                    if (!vis[s]) {
                        Q.push(Node{s, p, cur.n + M[to][i]});
                        vis[s] = true;
                    }
                }
        }
        printf("Game %d: %d\n", ++kase, ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值