HDU1944 SNim (SG,博弈)

题目链接

分析:

SG模版。

详情请参见LCY的课件:http://acm.hdu.edu.cn/forum/read.php?tid=6875

AC代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 100 + 10;

int sg[10000+10], a[maxn], k;

int mex(int n) {
    bool vis[maxn] = {false};

    for(int i=0; i<k; i++) {
        int t = n - a[i];
        if(t < 0) continue;
        if(sg[t] == -1) sg[t] = mex(t);
        vis[sg[t]] = true;
    }

    for(int i=0; ; i++) if(!vis[i]) return i;
}

int main() {
    int m;
    while(cin >> k && k) {
        string s;

        for(int i=0; i<k; i++) cin >> a[i];
        cin >> m;

        memset(sg, -1, sizeof(sg));

        while(m--) {
            int j, ans = 0, hi;
            cin >> j;

            while(j--) {
                cin >> hi;
                ans ^= mex(hi);
            }

            if(ans) s += 'W';
            else s += 'L';
        }

        cout << s << endl;
    }

    return 0;
}
原文地址:https://www.cnblogs.com/tanhehe/p/3111181.html