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

题意:

有n堆石子,每堆石子里面至少有一个石子,有A、B两人。A先取,取完所有石子的一方获胜,问当双方都采取最优策略时,谁能获胜。

思路:

nim博弈模板,谁面临平衡态势谁就会输。

特判一种情况:当每一堆石子的个数全部都为1的时候,这个时候只能每次拿一个,根据1的奇偶性进行判断。

 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     int t,tt=1;
 6     scanf("%d",&t);
 7     while(t--)
 8     {
 9         int n,xx;
10         scanf("%d",&n);
11         int x=0,flag=0;
12         for(int i=0; i<n; i++)
13         {
14             scanf("%d",&xx);
15             if(xx!=1)
16             {
17                 flag=1;
18             }
19             x^=xx;
20         }
21         if(flag==0)
22         {
23             if(n%2==0)
24                 printf("Case %d: Alice\n",tt++);
25             else
26                 printf("Case %d: Bob\n",tt++);
27         }
28         else
29         {
30             if(x!=0)
31                 printf("Case %d: Alice\n",tt++);
32             else
33                 printf("Case %d: Bob\n",tt++);
34         }
35     }
36     return 0;
37 }
View Code

猜你喜欢

转载自www.cnblogs.com/OFSHK/p/11589160.html