POJ 1704-Georgia and Bob棋子移动(Nim博弈)

来源:互联网 发布:易通网络vpn加速器 编辑:程序博客网 时间:2024/05/22 17:57

Georgia and Bob

Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 8996 Accepted: 2892

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 <= 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

231 2 381 5 6 7 9 12 14 17

Sample Output

Bob will winGeorgia will win

Source

POJ Monthly--2004.07.18

题目意思:

排成直线的格子上有n个棋子。棋子i在左数第pi个格子中。
Georgia和Bob轮流选择一个棋子进行移动。每次可以移动一格及以上任意多格,但是不可以反超其他棋子,也不允许将两个棋子放入同一个格子内。
无法再次进行操作的一方判定为输掉比赛。假设Georgia先进行移动,当双方都采取最优策略时,谁会获胜?

解题思路:

将游戏转换成Nim游戏。
棋子从左往右两辆组成一对,每对都看成Nim游戏中的一堆石子,两棋子之间的间隔就是每堆石子的个数。
当棋子个数为奇数的时候,在最右边加一个与最后一个棋子距离为0的棋子。


#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>using namespace std;#define MAXN 1010int n,p[MAXN];void solve(){    if(n%2) p[n++]=0;//n为奇数时要补成偶数方便配对    sort(p,p+n);    int res=0;    for(int i =0;i<n-1;i+=2)        res^=(p[i+1]-p[i]-1);//两棋子之间的距离    if(!res) puts("Bob will win");    else puts("Georgia will win");}int main(){    int t;    cin>>t;    while(t--)    {        cin>>n;        for(int i=0; i<n; ++i)            cin>>p[i];        solve();    }    return 0;}



0 0