POJ 1740--A New Stone Game(博弈)

原題連接:http://poj.org/problem?id=1740

A New Stone Game
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6327 Accepted: 3455

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

題意:有N堆石子,兩人輪流進行操作,每一次爲“操作者指定一堆石子,先從中扔掉一部分(至少一顆,可以全部扔掉),然後可以將該堆剩下的石子中的任意多顆任意移到其他未取完的堆中”,操作者無法完成操作時爲負。

 分析:
     只有一堆時先手必勝。
     有兩堆時若兩堆相等則後手只用和先手一樣決策即可保證勝利,後手必勝。若不同則先手可以使其變成相等的兩堆,先手必勝。
     有三堆時先手只用一次決策即可將其變成兩堆相等的局面,先手必勝。
     有四堆時由於三堆必勝,無論先手後手都想逼對方取完其中一堆,而只有在四堆都爲一顆時纔會有人取完其中一堆,聯繫前面的結論可以發現,只有當四堆可以分成兩兩相等的兩對時先手纔會失敗。                   

     分析到這裏,題目好像已經有了一些眉目了,憑藉歸納猜想,我們猜測必敗態的條件爲“堆數爲偶數(不妨設爲2N),並且可以分爲兩兩相等的N對”。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 105;
int n;
int p[maxn];

int main(){
	while(~scanf("%d", &n)){
		memset(p, 0, sizeof(p));
		if(n == 0) break;
//		cout<<123<<endl;
		for(int i=0; i<n; i++){
			int temp;
			cin>>temp;
			p[temp]++;
		}
		int flag=0;
		if(n & 1){
			cout<<1<<endl;
			continue;
		}
		else {
			for(int i=1; i<=100; i++){
				if(p[i] & 1){
					cout<<1<<endl;
					flag = 1;
					break;
				}
			}
		}
		if(!flag) cout<<0<<endl;
	}
}

發佈了41 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章