POJ A New Stone Game

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. 

题目大意:

这道题英文写的题目题意不是很清楚,以下是比较靠谱的题目:
有N堆石头,每堆石头数目在1到100之间,最多有10堆.两人分别取走石头.取石头的规则是:每次只能从1堆中取,每次取走至少1个。取过后还可以把这堆的石头任意分配到其它石子数不为0的堆上,当然也可以不分配。问给定这些石头堆的情况,两人轮流取,谁取走最后一个石子谁胜利,问是先取的胜利还是后取的胜利.双方最优策略。
输入有多组数据。
每组数据第一行为一个整数n(1<=n<=10),第二行有n个数,表示初始时每堆的石子个数。

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

题解:

这是我第一道博弈论的题目,所以看了题解。但是从之前看的一些文章中,感觉关于胜败状态(如Nim之类的)的博弈论,好像都可以从“只有两堆且两堆数量相同”这种状态开始分析。

首先要两个博弈论的性质:
一个状态为必胜状态当且仅当它的后继状态至少有一个必败状态。     ①
一个状态为必败状态当且仅当它的后继状态全为必胜状态                    

首先只有一堆时,先手必胜。
接下来讨论“石头只有两堆且两堆数量相同”的情况(下文简称“2”状态),例如“1,1”这种情况。和Nim游戏相同,在这种情况下先取的必输:只要两人在与对手不同的堆上,做与对手相同的操作。这样就相当于两人轮流 取走相同个数的 石子,这样后手必胜。

接下来可推出:
当只有三堆时,先手可通过一次处理出“2”状态,这样先手必胜。
有四堆时由于三堆必胜,无论先手后手都想逼对方取完其中一堆,显然:只有在四堆都为一颗时才会有人取完其中一堆。联系前面的结论①可以发现:当且仅当四堆可以分成两两相等的两对时(x,x,y,y)先手必败。   ③

知道这个结论后,就可以把N堆中两两相等的堆去掉,来讨论互不相等的堆来。
第一,只有一堆x,第一个人直接全部取走就胜利了。(显然x,y,y的情况也是第一人胜,所以忽略相等的石头)。
第二,x,y的形式(这里不妨假设递增,下同)。第一人从第二堆中取走(y-x)个石头,这样两堆相等,最终还是第一人胜。
第三,x,y,z的形式.第一人从最后一堆中取走(z+x-y)个石头,再将(y-x)个石头移到第一堆上(z>y-x一定成立),这样还是第一人胜。
依此类推,移动个数最多的石头堆然后再分配总可以前面变成两两相等的情况.可见只要开始不全是两两相等,那先取者必胜。

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int n,a[12];
int main()
{
	while(scanf("%d",&n)&&n)
	   {for(int i=1;i<=n;i++)
	      scanf("%d",&a[i]);
	    if(n%2==1) printf("1\n");
	    else
	       {int i;
			sort(a+1,a+n+1);
		    for(i=1;i<=n;i=i+2)
		       {if(a[i]!=a[i+1]) {printf("1\n"); break;}
			   }
			if(i>n) printf("0\n");
		   }
	   }
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值