阶梯博弈-poj1704

1.问题描述:(网址:http://poj.org/problem?id=1704

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

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

Sample Output

Bob will win
Georgia will win

Source

POJ Monthly--2004.07.18

问题的大概意思是:Bob和Georgia轮流移动棋子,并且Georgia先移动棋子,并且只能向左边移动棋子,到最后不能够移动棋子的人那么就位输。输入:第一行给出测试数据的数量,第二行给出第一组测试用例的哪些地方存在着棋子....第n组第一组测试用例的哪些地方存在着棋子

2.思路:观察题目可以知道这是Nim游戏的变体,也属于博弈的问题

虽然棋子与棋子中间存在着空格,但是在移动的过程中总是可以将其中的间隔消掉的,那么这里的间隔对题目是没有影响的:先手移动多少个后手也跟着移动多少个最后间隔就消掉了,有影响的是每两个棋子与棋子之间的间隔,

假如有位置的棋子的数量为偶数,那么只需要考虑每两个棋子之间的距离,进行异或运算之后判断结果为零则先手输

假如为奇数的话第一个棋子与起始位置之间存在着一定的距离可以移动,需要进行异或运算

可以举一些简单的例子自己在纸上画一下那么对博弈问题的理解会比较深刻

3. 代码如下:
import java.util.Arrays;
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int caseNum = sc.nextInt();
        int a[][] = new int [caseNum][];
        for(int i = 0; i < caseNum; i++){
            int n = sc.nextInt();
            a[i] = new int[n];
            for(int j = 0; j < n; j++){
                a[i][j] = sc.nextInt();
            }
        }
        for(int i = 0; i < caseNum; i++){
            solve(a[i]);
        }
    }

    private static void solve(int[] a) {
        Arrays.sort(a);
        //判断是偶否为奇数
        int len = a.length;
        int res = 0;
        if((len & 1) == 1){
            for(int i = 0; i < len; i += 2){
                //一定要考虑是奇数的情况
                //减去1是因为两个棋子之间的距离
                //假如是奇数的话第一个位置如果有数字的话那么减去一之后就变成了零
                res ^= (i == 0) ? (a[i] - 1) : a[i] - a[i - 1] - 1;
            }
        }else{
            for(int i = 1; i < len; i += 2){
                res ^= a[i] - a[i - 1] - 1;
            }
        }
        if(res == 0){
            System.out.println("Bob will win");
        }else{
            System.out.println("Georgia will win");
        }
    }
}
 

可以举一些简单一点的例子进行验证,当为奇数的时候那么当后边的偶数对不能够移动之后那么第一个棋子与起始位置的距离可能可以进行移动

最简单的测试用例是:

① 1 3 3 5 7:为奇数个,在3 5 7 的位置上分别有棋子

当3 5 7 的位置堆在一起的时候那么第一个棋子与起始位置上有空位那么最终是Bob胜出

② 1 4 1 3 5 7 : 为偶数个, 在1 3 5 7 的位置上分别有棋子

猜你喜欢

转载自blog.csdn.net/qq_39445165/article/details/82988781