HOJ 4388 Stone Game II

Problem Description

Louis and Lester decide to play a stone game. At the beginning of the game there are n piles of stones. Louis and Lester then take turns to remove the stones, starting from Louis.

At each step of the game, the player choose a pile and remove at least one stones from it. The game terminates when all stones are removed. The player who removes the last stone loses the game.

You are to write a program to determine which player will finally win the game, provided that both players do their best in the game.

Input

The input contains several test cases. Each test case consists of two lines. The first line contains an integer n (n<=10000), the number of piles, and the second line contains n integers, denoting the number of stone in each pile at the beginning of the game. You may assume that no pile will contain more than 1000 stones. Input is terminated by end of file.

Output

For each test case, if Louis win the game, print "Louis" on one line, otherwise print "Lester".

Sample Input

3
3 2 1
2
1 4

Sample Output

Lester
Louis

题意:有n堆石子,轮流取子,每次选一堆取任意数量,但要大于0,将石子取光的人输掉游戏,问先手能不能赢。

 

看了别人的题解才会写,网上这题好像也没几个人写题解,搜一下应该都能搜到,这里写一下自己学完后的个人思考。

思路:由于这和传统的取子游戏不一样,我们需要取特定的情况进行分析。

一、石子个数全为1,则当偶数堆时先手必胜,奇数堆先手必败

二、有一堆个数大于1,其余全为1,先手必胜。

接下来时重点,由于时取子游戏,我们可以利用nim和这个性质,nim和不为0的状态,我们一定可以通过一步操作使它变成nim和为0的状态。

三、两堆以上(包括)大于1,其余为1(注意这里其余为1的数量可能是0), nim == 0。

四、两堆以上(包括)大于1,其余为1(注意这里其余为1的数量可能是0), nim != 0。

 

我们可以发现状态四一定可以在保持两堆以上(包括)大于1的状态下转化为状态三,不懂的可以再去学一下nim游戏的原理,由于有个人语文水平不再过多描述,而状态三可以转化为四或二,在某些情况下只能转化为二,由于二是必胜态,所以如果石子一开始处于状态四,则先手必胜,状态三则先手必败,那么就很简单了,下面是代码

#include 
using namespace std;
const int maxn = 10000 + 50;
int n;
int a[maxn];
int sg[maxn];
int num[maxn];
int main(int argc, char const *argv[])
{
	while(~scanf("%d", &n)){
		int nim = 0;
		num[1] = 0;
		for(int i = 1; i <= n; i++){
			scanf("%d", &a[i]);
			num[a[i]]++;
			nim ^= a[i];
		}
		int flag = 0;
		if(nim == 0 && num[1] == n) flag = 1;
		if(nim != 0 && num[1] != n) flag = 1;
		if(flag == 0) printf("Lester\n");
		else printf("Louis\n");
	}
	return 0;
}

 

 

你可能感兴趣的:(博弈论)