HDU 4778 (状压DP 博弈)

题目链接:点击这里

题意:b个包,一共有g种颜色的宝石,s个同颜色宝石能炼一个石头。两个人轮流选择一个包把包里面的石头扔进炉子,如果当前把包里宝石扔进炉子的人得到石头就能继续扔包。A先手,求A最多领先B多少个石头。

dp[i][0/1] 表示包的状态是i,某个人先手一直到游戏结束A最多能领先多少个石头,然后状态从后往前推,如果当前回合是A先手必然是选择领先数+新获得石头数最大的转移路径,如果是B先手必然是选择领先数-新获得石头数最小的转移路径,一直推至 dp[0][0] ,复杂度 O(b×g×2b) .

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#pragma comment(linker, "/STACK:1024000000,1024000000") 
using namespace std;
#define INF 111111111

int dp[1<<21][2];
int a[22][11], sum[1<<21][11];
int g, b, s, n;
vector <int> num[22];

void init () {
    for (int i = 0; i < b; i++)
        num[i].clear ();
    for (int i = 0; i < (1<int tmp = 0, cur = i;
        while (cur) {
            tmp += (cur&1);
            cur >>= 1;
        }
        num[tmp].push_back (i);
    }
}

void dfs (int pos, int pre) { 
    //现在选择的 之前选择的状态
    if (pos == b) return ;
    dfs (pos+1, pre);//这个不选
    int now = (pre | (1<for (int i = 1; i <= g; i++) {
        sum[now][i] = (sum[pre][i]+a[pos][i]) % s;
    }
    dfs (pos+1, now);//这个选
}

int main () {
    while (scanf ("%d%d%d", &g, &b, &s) == 3 && (g+b+s)) {
        init ();
        memset (a, 0, sizeof a);
        for (int i = 0; i < (1<0] = -INF;
            dp[i][1] = INF;
        }
        dp[(1<1][0] = dp[(1<1][1] = 0;
        memset (sum, 0, sizeof sum);
        for (int i = 0; i < b; i++) {
            scanf ("%d", &n);
            for (int j = 0; j < n; j++) {
                int id;
                scanf ("%d", &id);
                a[i][id]++;
            }
        }
        dfs (0, 0);

        for (int i = b-1; i >= 0; i--) {
            int sz = num[i].size ();
            for (int j = 0; j < sz; j++) {
                int now = num[i][j];
                for (int bit = 0; bit < b; bit++) if (!((1<int next = (1<int more = 0;
                    for (int k = 1; k <= g; k++) {
                        more += (a[bit][k]+sum[now][k]) / s;
                    }
                    if (more) {
                        dp[now][0] = max (dp[now][0], dp[next][0]+more);
                        dp[now][1] = min (dp[now][1], dp[next][1]-more);
                    }
                    else {
                        dp[now][0] = max (dp[now][0], dp[next][1]);
                        dp[now][1] = min (dp[now][1], dp[next][0]);
                    }
                }
            }
        }
        printf ("%d\n", dp[0][0]);
    }
    return 0;
}

你可能感兴趣的:(状压DP,博弈)