lightoj 1253 - Misere Nim(nim博弈)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37943488/article/details/82526047

1253 - Misere Nim

    PDF (English) Statistics Forum
Time Limit: 1 second(s) Memory Limit: 32 MB

Alice and Bob are playing game of Misère Nim. Misère Nim is a game playing on k piles of stones, each pile containing one or more stones. The players alternate turns and in each turn a player can select one of the piles and can remove as many stones from that pile unless the pile is empty. In each turn a player must remove at least one stone from any pile. Alice starts first. The player who removes the last stone loses the game.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing an integer k (1 ≤ k ≤ 100). The next line contains k space separated integers denoting the number of stones in each pile. The number of stones in a pile lies in the range [1, 109].

Output

For each case, print the case number and 'Alice' if Alice wins otherwise print 'Bob'.

Sample Input

Output for Sample Input

3

4

2 3 4 5

5

1 1 2 4 10

1

扫描二维码关注公众号,回复: 3206231 查看本文章

1

Case 1: Bob

Case 2: Alice

Case 3: Bob

题意:n堆石头,每个人取一个或多个,问最后谁赢

看起来是一道很普通的nim博弈,但是会发现1的时候是不一样的,所以当石头数全是1的时候就特判一下,其他的就是普通的nim博弈

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int test;
    scanf("%d",&test);
    for(int cas=1;cas<=test;cas++)
    {
        int n;
        scanf("%d",&n);
        int ans=0;
        bool flag=false;
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            ans^=x;
            if(x!=1) flag=true;
        }
        if(!flag)
        {
            if(n&1) printf("Case %d: Bob\n",cas);
            else printf("Case %d: Alice\n",cas);
        }
        else
        {
            printf("Case %d: %s\n",cas,ans==0?"Bob":"Alice");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/82526047