HDU-1907 John (nim博弈)

题目链接

题意:

有n堆糖,每堆糖的个数是ai,每个人每次取一堆里的若干个,最后一个取完的人为负,求谁会赢

思路:

Nim博弈,对于每堆都是1的情况需要特判;对于不都是1的情况判断是否为奇异局势就可以了
更多博弈资料

#include <cstdio>
#include <algorithm>

using namespace std;

int a[50];
int main()
{
    int T;
    int n;
    scanf("%d",&T);
    while(T--)
    {
        int res = 0;
        bool f = true;
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
            res ^= a[i];
            if(a[i] > 1)
                f = false;
        }
        if(f)                 //每堆都是1
        {
            if(n % 2 == 0)              //判断堆数奇偶
                printf("John\n");
            else
                printf("Brother\n");
        }
        else                  //每堆不都是1
        {
            if(res == 0)               //奇异局势
                printf("Brother\n");
            else
                printf("John\n");
        }
    }
    return 0;
}

相似题目HDU-2509