您的位置:首页 > 其它

hdu-1944&& 1534 S-Nim

2014-06-02 10:49 316 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1944

题意:在普通的Nim游戏上加入一些限制。给定一个集合S, 每次只能取S的元素个石子。

题解:SG函数。

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: