题目
Description
Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, number the grids from left to right by
1
,
Georgia and Bob move the chessmen in turn. Every time a player will choose a chessman, and move it to the left without going over any other chessmen or across the left edge. The player can freely choose number of steps the chessman moves, with the constraint that the chessman must be moved at least ONE step and one grid can at most contains ONE single chessman. The player who cannot make a move loses the game.
Georgia always plays first since “Lady first”. Suppose that Georgia and Bob both do their best in the game, i.e., if one of them knows a way to win the game, he or she will be able to carry it out.
Given the initial positions of the n chessmen, can you predict who will finally win the game?
Input
The first line of the input contains a single integer
T
Output
For each test case, prints a single line, “Georgia will win”, if Georgia will win the game; “Bob will win”, if Bob will win the game; otherwise ‘Not sure’.
Sample Input
2
3
1 2 3
8
1 5 6 7 9 12 14 17
Sample Output
Bob will win
Georgia will win
题意
Georgia和Bob在玩游戏,排成直线的格子上放有
思路
棋子分为奇数和偶数两种情况.
当棋子个数为偶数时,我们就可以把棋子两辆看成一对,每对棋子就可以转化成 Nim 游戏里的一堆石子,每堆石子的个数就可以用棋子之间的间隔来表示。
右边的棋子向左移动就相当于从一堆石子里取出石子,但是左边的棋子向左移动两个棋子之间的间隔变大,就和 Nim 不相同了。但是,如果出现增加棋子间隔(增加石子)的情况:对手石子数增加,就让自己将所加部减回去就回到了之前的状态;反之,自己的石子数增加,那就让对手将所加的减回去就回到了之前的状态。
因此,这个游戏和 Nim 游戏的胜负是一样的。
当棋子的个数为奇数时,将最左边的棋子与格子的最左边的间隔看作是一堆石子,同样也可以转化成 Nim 。
Nim 游戏的胜负只要判断在轮到每个人进行操作时,每次都用异或运算,就可以判断游戏的胜负。如:
a1
XOR
a2
XOR
⋅⋅⋅
XOR
an
≠
0
a1
XOR
a2
XOR
⋅⋅⋅
XOR
an
=
至于题目里说的Not sure的情况,应该是不存在这种情况的。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<cstdlib>
#include<functional>
#include<climits>
#include<cctype>
#include<iomanip>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define mod 1e9+7
#define clr(a,x) memset(a,x,sizeof(a))
const double eps = 1e-6;
int t,n,a[10010];
void solve()
{
if(n%2) //判断棋子的个数是否为偶数
a[n++]=0; //是奇数,则新令一个棋子放在第一个位置上
sort(a,a+n);
int x=0;
for(int i=0;i<n-1;i+=2) //每两个棋子的选
{
x^=(a[i+1]-a[i]-1); //每一堆石子的个数就是两个棋子的间隔
}
if(x)
cout<<"Georgia will win"<<endl;
else
cout<<"Bob will win"<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
solve();
}
return 0;
}