博弈论(三)——#10245. 「一本通 6.7 练习 2」巧克力棒

题目链接:https://loj.ac/problem/10245
解题思路
需要对尼姆博弈有深入的了解,这题如果不用取巧克力,就是典型的尼姆博弈。我们知道,尼姆博弈,如果异或和为0则是P态,所以,如果先手拿出几根巧克力异或和不为0,后手就可以使异或和变为0,此时先手再拿,后手又可以通过操作使异或和变为0。所以,先手要想取胜,必须先拿出最大的异或和为0的集合,此时后手无论怎么操作,都会使异或和变为不等于0。所以,如果有异或和为0的集合,先手必胜。如果没有,先手必输。因为n很小,所以直接暴搜判断即可。
AC代码

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
int n;
int l[15];
bool dfs(int i,int sum,int x)
{
    if(x&&!sum)
    return 1;
    if(i>n)
    return 0;
    if(dfs(i+1,sum,x))
    return 1;
    if(dfs(i+1,sum^l[i],x+1))
    return 1;
    return 0;
}
int main()
{
    for(int i=1;i<=10;++i)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
        scanf("%d",&l[i]);
        if(dfs(1,0,0))
        puts("NO");
        else
        puts("YES");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44491423/article/details/108436141