山东济南彤昌机械科技有限公司 山东济南江鹏工贸游有限公司

poj 1740 A New Stone Game(博弈)

  
                                  A New Stone Game
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5338   Accepted: 2926

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

3
2 1 3
2
1 1
0

Sample Output

1
0

Source

 

 

【思路】

     博弈

  当n=1时,先手必胜;

  当n=2时,如果两堆相等,先手取后只剩一堆则局面必胜,先手取后剩两堆,则后手可以始终维持两堆相等直到(1,1),此时必由先手完成第一种情况。因此先手必败。

  当n=3时,若a1<=a2<=a3,则先手可以通过操纵a3使得剩下两堆为(a2,a2),此时局面必败。因此先手必胜。

  推广:

  当n为奇数时,若a1<=a2…<=an-1<=an,则操作an使得a1->a2,a3->a4….an-2->an-1,因为项为正,所以an>=a2-a1+…an-1-an-2。因此先手必胜。

  当n为偶数时:若a1=a2,a3=a4…an-2=an-1,则先手必败;否则总可以操作an使得an=a1,a2=a3…an-2=an-1,an-a1>=a2-a2+…an-1-an-2,此时先手必胜。

 

【代码】

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 int n,a[11];
 6 
 7 int main() {
 8     while(scanf("%d",&n)==1 && n) {
 9         for(int i=1;i<=n;i++) scanf("%d",&a[i]);
10         if(n&1) puts("1");
11         else {
12             sort(a+1,a+n+1);
13             int flag=1;
14             for(int i=1;i<=n;i+=2)
15                 if(a[i]!=a[i+1]) { flag=0; break; }
16             if(flag) puts("0"); else puts("1");
17         }
18     }
19     return 0;
20 }

 

posted on 2016-01-30 11:11  hahalidaxin  阅读(211)  评论(0编辑  收藏  举报