poj1740 A New Stone Game----分析P/N
A New Stone Game
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4111 Accepted: 2179
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
LouTiancheng@POJ
[cpp]
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int a[12];
int main ()
{
int n, i;
while (scanf("%d", &n) != EOF && n)
{
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
if (n % 2)printf("1\n");
else
{
sort(a, a + n);
for (i = 0; i < n - 1; i += 2)
if (a[i] != a[i + 1])
{
printf("1\n");
break;
}
if (i >= n)printf("0\n");
}
}
}
题意:给你n堆石子,每次可以从任意一堆中取走任意颗石子(至少一颗)扔掉,然后从剩下的石子中取任意颗石子加到任意其他堆未取完的堆中,问先手是否有取胜策略,如果有,输出1,否则输出0.
这题主要是寻找必败态。
如果只有一堆石子,先手必胜。
如果有两堆石子,并且两堆石子的数量相等,那么先手采取什么样的策略,后手采取一样的策略,先手必败。
如果有三堆石子,那么先手可以在第一步取到只剩两堆相同数量的石子,先手必胜。
如果有四堆石子,由于三堆石子是必胜态,所以无论是先手还是后手都想逼对方取完某一堆石子,只有在四堆石子都为1时,擦能迫使某一方取完一堆石子,通过分析可知,只有当四堆石子可以分成两两相等的两队时,先手必败。
由上我们可以猜测,n 堆石子,当且仅当n为偶数,且n对石子可以分成n/2对两两相等的石子时,先手必败。
证明:
第一条性质:终点是必败态,满足。
第二条性质:必胜点一定有种策略可以进入必败态。
由以上可知,必胜点是当n为奇数或者n为偶数且不能分成n/2对两两相等的石子。
当n为奇数时我们一定可以把其中一堆分到其他堆中,使成n/2对两两相等的石子,第二种情况也是成立的。
第三条性质:必败点只能到达必胜点。显然成立
作者:qiqijianglu
补充:软件开发 , C++ ,