Fork me on GitHub

POJ 1704 Georgia and Bob (Nim博弈)

题目

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

1
2
3
4
5
2
3
1 2 3
8
1 5 6 7 9 12 14 17

Sample Output

1
2
Bob will win
Georgia will win

题意

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


思路

棋子分为奇数和偶数两种情况.

当棋子个数为偶数时,我们就可以把棋子两辆看成一对,每对棋子就可以转化成 $Nim$ 游戏里的一堆石子,每堆石子的个数就可以用棋子之间的间隔来表示。

右边的棋子向左移动就相当于从一堆石子里取出石子,但是左边的棋子向左移动两个棋子之间的间隔变大,就和 $Nim$ 不相同了。但是,如果出现增加棋子间隔(增加石子)的情况:对手石子数增加,就让自己将所加部减回去就回到了之前的状态;反之,自己的石子数增加,那就让对手将所加的减回去就回到了之前的状态

因此,这个游戏和 $Nim$ 游戏的胜负是一样的。

当棋子的个数为奇数时,将最左边的棋子与格子的最左边的间隔看作是一堆石子,同样也可以转化成 $Nim$ 。

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

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

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

至于题目里说的Not sure的情况,应该是不存在这种情况的。


代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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;
}
-------------本文结束感谢您的阅读-------------
obsidian wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持是我最大的动力!
  • 本文作者: obsidian
  • 本文链接: http://ooobsidian.github.io/2017/08/06/POJ 1704/
  • 版权声明: 本博客所有文章除特别声明外,均为作者测试键盘性能与显示器性能所用,文章内容为随机生成,如有雷同,不胜荣幸。转载请注明出处!