Georgia and Bob
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
思路:
- 若棋子的个数为偶数,可以将棋子每两个之间的空格数分为一堆,一共有n堆
- 变形的问题就是谁先能将堆中的空格先拿完的问题
- 若棋子的个数为奇数,也是一样的思路,只不过第一个棋子独自分为一堆
- 这就是变形的Nim问题(阶梯Nim博弈)
- 为什么我们不考虑堆之间的空格的个数呢?
- 下面用一个图来解释一下:
- 若棋子一开始摆放的位置如图所示,我们现在能移动的棋子只有4和6号棋子,若你是移动4号棋子,你是必输的,所以你只能移动6号棋子,之后对手的选择只能移动4号棋子
- 我们发现4号和6号棋子之间的空格数并没有变化,所以不考虑堆之间的空格数
- 坑点:输入的棋子位置是乱序的
AcCode:
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main{ public static void judge(List<int[]> list,int index) { int res = 0; int[] nums = list.get(index); if((nums.length&1)==0) {//若有偶数个棋子 for (int i = 0; i < nums.length-1; i+=2) { res = res^(nums[i+1]-nums[i]-1); } }else {//若有奇数个棋子 res = res^(nums[0]-1); for (int i = 1; i < nums.length-1; i+=2) { res = res^(nums[i+1]-nums[i]-1); } } if(res==0) { System.out.println("Bob will win"); }else { System.out.println("Georgia will win"); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); List<int[]> list = new ArrayList<int[]>(); int T = in.nextInt(); int n; int[] nums = null; int res = 0; while(T!=0) { n = in.nextInt(); nums = new int[n]; for (int i = 0; i < nums.length; i++) { nums[i] = in.nextInt(); } Arrays.sort(nums); list.add(nums); T--; } for (int i = 0; i < list.size(); i++) { judge(list, i); } } }