POJ 1704 Georgia and Bob (Nim游戏变形)

题目:http://poj.org/problem?id=1704

思路:Nim游戏策略,做如下转换,如果N是偶数,则两两配对,将两个数之间的格子数(距离)看做成这一堆石头的数量。

如果N是奇数,则将一个0的格子放在第一个。

代码:

#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=10000+2;
int N,P[MAXN];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>N;
		for(int i=0; i<N; i++)
		{
			cin>>P[i];
		}
		if(N&1==1) P[N++]=0;
		sort(P,P+N);
		int res=0;
	    for(int i=0; i+1<N; i+=2)
	    {
    		res ^= (P[i+1]-P[i]-1);
    	}
    	if(res==0) cout<<"Bob will win"<<endl;
    	else cout<<"Georgia will win"<<endl;
	}
	return 0;
}


原文地址:https://www.cnblogs.com/james1207/p/3339655.html