obsidian

POJ 1074 Georgia and Bob(Nim博弈)

06
2017/08

# 题目

## 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$ , $2$ , $3$ , ..., and place $N$ chessmen on different grids, as shown in the following figure for example:

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$ $(1 \leq T \leq 20)$ , the number of test cases. Then $T$ cases follow. Each test case contains two lines. The first line consists of one integer $N$ $(1 \leq N \leq 1000)$ , indicating the number of chessmen. The second line contains $N$ different integers $P_1$ , $P_2$ ... $P_n$ $(1 \leq P_i \leq 10000)$ , which are the initial positions of the $n$ chessmen.

## 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在玩游戏，排成直线的格子上放有 $n$ 个棋子。棋子 $i$ 在从左向右的第 $p_i$ 个格子上。两人轮流选择一个棋子向左移动，每次可以移动任意一格，但是不能超过其他的棋子，也不允许两个棋子放在一个格子里。假设二人足够聪明，当一方无法再进行移动棋子时，该方失败，Georgia先进行移动，问在每种条件下谁会获胜？

# 思路

$Nim$ 游戏的胜负只要判断在轮到每个人进行操作时，每次都用异或运算，就可以判断游戏的胜负。如：

$a_1$ XOR $a_2$ XOR $···$ XOR $a_n$ $\neq$ $0$ $\Rightarrow$ 必胜态

$a_1$ XOR $a_2$ XOR $···$ XOR $a_n$ $=$ $0$ $\Rightarrow$ 必败态

# 代码

#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;
}


Last modification：November 2nd, 2019 at 12:39 am