【杭电oj】1536 - S-Nim(SG函数)

题目链接

题意:类似经典的Nim博弈,不同的是这个题限制了可使用的操作,给出了操作集合。于是我们根据操作集合先做出SG数组就行了。

WA了一遍,有一个坑点就是给出的操作不一定是升序的,需要先排下序。。。
感觉自己是个傻子。

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e4+100;
int op[110],sg[11000];
int k,T,n,x;
vector<int > s;
void getSG(){
    sg[0] = 0;
    for(int i=1;i<=N;++i){
        s.clear();
        for(int j=1;i>=op[j] && j<=k;++j)
            s.push_back(sg[i - op[j]]);
        for(int j=0;;++j){
            if(count(s.begin(),s.end(),j) == 0){
                sg[i] = j;
                break;
            }
        }
    }
}
int main(int argc, char const *argv[])
{
    while(scanf("%d",&k) && k){
        for(int i=1;i<=k;++i)   scanf("%d",&op[i]);
        sort(op+1,op+1+k);
        getSG();
        scanf("%d",&T);
        while(T--){
            scanf("%d",&n);
            int XOR = 0;
            for(int i=1;i<=n;++i){
                scanf("%d",&x);
                XOR ^= sg[x];
            }
            if(XOR) printf("W");
            else printf("L");
        }
        puts("");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41009682/article/details/82291865
今日推荐