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 (1T20) , the number of test cases. Then T cases follow. Each test case contains two lines. The first line consists of one integer N (1N1000) , indicating the number of chessmen. The second line contains N different integers P1, P2 Pn (1Pi10000) , 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个格子上。两人轮流选择一个棋子向左移动,每次可以移动任意一格,但是不能超过其他的棋子,也不允许两个棋子放在一个格子里。假设二人足够聪明,当一方无法再进行移动棋子时,该方失败,Georgia先进行移动,问在每种条件下谁会获胜?


思路

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

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

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

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

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

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

a1 XOR a2 XOR XOR an 0 必胜态

a1 XOR a2 XOR XOR an = 0 必败态

至于题目里说的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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值