POJ 1740(构造博弈)

题目链接:

https://cn.vjudge.net/problem/POJ-1740

题目描述:

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
 1 /* 
 2 题意描述
 3 有n堆石子,每堆石子有pi个,两人轮流操作,每人每次选择一堆,至少移除一个,至多取完这一堆,然后移动该堆剩下的石子到其他任意堆
 4 (只要该堆还有石子),最后一个取完石子者胜,问都采取最优策略最后谁赢。 
 5 构造博弈
 6 先来看看一般规律
 7 有一堆,先手必胜(直接取完)
 8 有两堆,若两堆数目相同,为奇异局势,后手只需采取和先手相同的策略,再次构造奇异局势,最后先手必败。
 9         若两堆数目不同,为非奇异局势,先手构造奇异局势,造成后手必败。
10 有三堆,先手设法构造奇异局势,先选择一堆,移除一定数目的石子,剩下的补充到其他堆,造成对手面对奇异局势,从而必胜。
11 有四堆,分成两个两堆,如果至少存在一对数目不同,先手构造奇异局势,可造成后手必败(回到有两堆的情况),否则,也就是都能凑成奇
12 异局势,而先手面临的全是奇异局势,则必败。
13 可以发现,当有奇数堆的时候,先手总是有机会选取一堆石子,构造至少一个奇异局势,从而必胜。
14 具体实现
15 奇数堆,先手必胜
16 偶数堆,至少存在一个非奇异局势,先手必胜,否则必败。 
17 */
18 #include<cstdio>
19 #include<algorithm>
20 using namespace std;
21 
22 const int maxn=10+10;
23 int main()
24 {
25     int n,p[maxn];
26     while(scanf("%d",&n) == 1 && n != 0){
27         for(int i=0;i<n;i++)
28             scanf("%d",&p[i]);
29         
30         if(n & 1)
31             puts("1");
32         else{
33             sort(p,p+n);
34             int i;
35             for(i=1;i < n; i+=2){
36                 if(p[i] != p[i-1]){
37                     puts("1");
38                     break;
39                 }
40             }
41             
42             if(i == n+1)
43                 puts("0");
44         }
45     }
46     return 0;
47 } 


 
原文地址:https://www.cnblogs.com/wenzhixin/p/9338070.html