zoukankan      html  css  js  c++  java
  • Misere Nim

    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

    3

    4

    2 3 4 5

    5

    1 1 2 4 10

    1

    1

    Sample Output

    Case 1: Bob

    Case 2: Alice

    Case 3: Bob

    思路:这道题就是典型的nim游戏博弈题。如果条件满足a1^a2^...^an = 0,则a赢(先取完最后一个石子的人输)。反之则b赢。

              特判:如果每堆石子都只有一个石头,那奇数堆的时候b赢,否则a赢。

    详细解释请看上一篇博客(傻笑.jpg)

    #include <cstdio>
    #include <iostream>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <cmath>
    #include <stack>>
    using namespace std;
    #define ll long long
    const int inf = 0x3f3f3f3f;
    const  ll linf  =1LL<<50;
    const int maxn = 1e5+8;
    int t, n, x, sum;
    int main()
    {
        scanf("%d", &t);
        int miao = t;
        while(t--)
        {
            sum = 0;
            bool flag = 1;
            scanf("%d", &n);
            for(int i = 0; i<n; i++)
            {
                scanf("%d", &x);
                if(x != 1)flag =0;
                sum ^= x;
            }
            if(flag)//如果所有的石碓都是只有一个石子
            {
                if(n&1)printf("Case %d: Bob
    ", miao-t);//如果堆数为奇数,则b赢
                else printf("Case %d: Alice
    ", miao-t);
            }
            else
            {
                if(!sum) printf("Case %d: Bob
    ", miao-t);//如果这个状态为非平衡状态,则b赢
                else printf("Case %d: Alice
    ", miao-t);
            }
        }
        return 0;
    }

     

  • 相关阅读:
    3-05. 寻求倒数第二链线性表K项目(15)(STL list应用 ZJU_PAT)
    springbatch操作CSV文件
    oracle 数据库技术支持生命周期表
    调试经验--硬盘U菜
    hdu149850 years, 50 colors (多个最小顶点覆盖)
    POJ3213(矩阵乘法)
    Cocos2d-x 2.3.3版本 FlappyBird
    POJ 2114 Boatherds 划分树
    jQuery 添加 删除 改动select option
    STL容器存储的内容动态分配情况下的内存管理
  • 原文地址:https://www.cnblogs.com/RootVount/p/10765180.html
Copyright © 2011-2022 走看看