zoukankan      html  css  js  c++  java
  • LightOJ 1253 Misere NIM(反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博弈板题;(anti-NIM博弈见前面博弈总结)知道:先手必胜的状态有两种: 1:全是一个,且ans=0;     

    2 : 不全是一个,且ans!=0;(ans为所有的异或和);

    其他的情况均为先手必败;

    参考代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int T,n,x,ans,cnt;
     4 int main()
     5 {
     6     scanf("%d",&T);
     7     for(int cas=1;cas<=T;cas++)
     8     {
     9         scanf("%d",&n);
    10         cnt=ans=0;
    11         for(int i=1;i<=n;++i)
    12         {
    13             scanf("%d",&x);
    14             if(x==1) cnt++;
    15             ans^=x;    
    16         }    
    17         if(cnt==n)
    18         {
    19             if(ans==0) printf("Case %d: Alice
    ",cas);
    20             else  printf("Case %d: Bob
    ",cas);
    21         } 
    22         else
    23         {
    24             if(ans) printf("Case %d: Alice
    ",cas);
    25             else printf("Case %d: Bob
    ",cas);
    26         }
    27     } 
    28     
    29     
    30     return 0;
    31 }
    View Code
  • 相关阅读:
    jvm003-运行时数据区及程序计数器
    jvm002-类加载子系统
    jvm001-JVM与Java体系结构
    (四十三)常用 10 种算法——马踏棋盘算法
    (四十二)常用 10 种算法——弗洛伊德算法
    node 环境配置
    cordova platform add android报错问题处理
    WPF代码引用Resouces中的图片
    WPF中利用WebClient向服务器上传文件
    未能加载文件或程序集“COM.Excel”或它的某一个依赖项
  • 原文地址:https://www.cnblogs.com/csushl/p/10388775.html
Copyright © 2011-2022 走看看