首页 > 代码库 > 博弈论5

博弈论5

A New Stone Game

 

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4177 Accepted: 2227

 

Description

 

Alice and Bob decide to play a new stone game.At the beginning of the game they pick n(1<=n<=10) piles of stones in a line. Alice and Bob move the stones in turn. At each step of the game,the player choose a pile,remove at least one stones,then freely move stones from this pile to any other pile that still has stones. For example:n=4 and the piles have (3,1,4,2) stones.If the player chose the first pile and remove one.Then it can reach the follow states. 2 1 4 2 1 2 4 2(move one stone to Pile 2) 1 1 5 2(move one stone to Pile 3) 1 1 4 3(move one stone to Pile 4) 0 2 5 2(move one stone to Pile 2 and another one to Pile 3) 0 2 4 3(move one stone to Pile 2 and another one to Pile 4) 0 1 5 3(move one stone to Pile 3 and another one to Pile 4) 0 3 4 2(move two stones to Pile 2) 0 1 6 2(move two stones to Pile 3) 0 1 4 4(move two stones to Pile 4) Alice always moves first. Suppose that both Alice and Bob do their best in the game. You are to write a program to determine who will finally win the game.

 

Input

 

The input contains several test cases. The first line of each test case contains an integer number n, denoting the number of piles. The following n integers describe the number of stones in each pile at the beginning of the game, you may assume the number of stones in each pile will not exceed 100. The last test case is followed by one zero.

 

Output

 

For each test case, if Alice win the game,output 1,otherwise output 0.

 

Sample Input

 

32 1 321 10

 

Sample Output

 

10

 

分析:

题意理解:①删除一个元素后,可以不移动;②谁把石子取完,谁就赢;③石子的各种移动方法题中的例子说得很清楚

思路分析:先看例子:

 

(1,1)  先手取一个,对手取完,先手输 (1,2)  先手可以取一个,留给对手(1,1),则先手赢 (1,3)  先手取两个,留给对手(1,1),则先手赢 ······ (2,2)  先手取一个后,如果移动得(3,0),对手取完,先手输;如果不移动,对手在另一堆取同样多得(1,1),先手也输 (2,3)  先手取一个得(2,2),先手赢 ······ 两堆的规律就显然了,堆数一样,先先手输;否则先手赢 三堆呢? (1,1,2)  先手取了2,就得(1,1),先手赢 (1,2,3)  先手可以在堆3中取去2,然后移动一个到第一堆,得(2,2),先手赢 ······ 可见,三堆的话,先手赢。那就可以由以上规律,大胆猜测了 ①奇数堆,就是先手赢? 比如(1,2,3,4,5)  则可以把5分配给前4个,变成 得(2,2,4,4) ②偶数堆,如果满足a1=a2,a3=a4,…………an-1=an,则先手输(因为可以分成两个两个地解决);否则先手赢 比如(1,2,3,4,5,6)  则可以把6分配给 2 ~ 5,6自己变成1,总体变成(1,1,3,3,5,5),先手赢

 1 # include<stdio.h> 2 # include<algorithm> 3 using namespace std; 4 int main() 5 { 6     int n,i; 7     int sign[12]; 8     int leap; 9     while(scanf("%d",&n)&&n)10     {11     for(i=0;i<n;i++)12         scanf("%d",&sign[i]);13     if(n%2==1)14     {15         printf("1\n");16         continue;17     }18     sort(sign,sign+n);19     leap=0;20     for(i=0;i<n;i+=2)21     {22         if(sign[i]!=sign[i+1])23         {24             leap=1;25             break;26         }27     }28     if(leap)29         printf("1\n");30     else printf("0\n");31     }32     return 0;33 }

 

博弈论5