POJ1704 Georgia and Bob(博弈)(Staircase Nim)

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 <= T <= 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 <= N <= 1000), indicating the number of chessmen. The second line contains N different integers P1, P2 ... Pn (1 <= Pi <= 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在左数第pi个格子上。Georigia和Bob轮流选择一个棋子向左移动。每次可以移动一格及以上任意多格,但是不允许反超其他的棋子,也不允许将两个棋子放在同一个格子内。

无法进行移动操作的一方失败。假设Georgia先进行移动,当双方都采取最优策略时,谁会获胜?


解题思路:

如果将棋子两两成对当作整体考虑,我们就可以把这个游戏转为Nimble游戏。先按棋子个数的奇偶分情况讨论。首先,考虑棋子个数为偶数的情况。把棋子从前往后两两组成一对,那么,我们就可以将没对棋子看成Nim中的一堆石子。石子堆中的石子的个数等于两个棋子之间的间隔。

让我们想想看为什么能够这样转换。考虑其中的某一对棋子,将右边的棋子向左移动就相当于从Nim的石子堆中取走石子。

另一方面,将左边的棋子向左移动,石子的数量就增加了。这就与Nim不同了。但是,即便对手增加了石子的数量,只要将所加部分减回去就回到了原来的状态;即便自己增加了石子的数量,只要对手将所加的部分减回去也回到了原来的状态。因此,该游戏的胜负状态和转移成的Nim的胜负状态是一致的。


当棋子的个数为奇数时,对最左边的棋子进行特殊处理后(即从第1个格子到最左边的棋子所在的格子作为第一对),同样可以转成Nim。


AC代码:

#include<stdio.h>
#include<algorithm>
using namespace std;

const int maxn=1000;

int n,p[maxn];

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%d",&p[i]);
		}
		if(n%2!=0) p[n++]=0;//对奇数特殊处理 
		sort(p,p+n);
		int x=0;
		for(int i=0;i+1<n;i+=2)
		{
			x^=(p[i+1]-p[i]-1);
		}
		if(x==0) printf("Bob will win\n");
		else printf("Georgia will win\n");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值