Description
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
Output
Sample Input
2 3 1 2 3 8 1 5 6 7 9 12 14 17
Sample Output
Bob will win Georgia will win
解題思路:
對相鄰棋子進行兩兩配對。比如有6個棋子,就配成3對,若是奇數個棋子,就把第一個單獨拿出來。配對後,相鄰兩對棋子間的距離對問題的解決是不構成影響的。比如第一對的後一個棋子走了a步,第二對棋子的前一個可以先走a步抵消影響後再選擇自己的步數。這樣就轉化成了給幾段步數,選擇一段後再選擇步數,看誰先走完。典型的尼姆博弈,不過多贅述。
AC代碼:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1005;
int main()
{
int t, n, sum, a[maxn];
scanf("%d", &t);
while(t--)
{
sum = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
int ans = 0;
if(n % 2 == 0)
for(int i = 1; i < n; i += 2)
ans ^= a[i] - a[i - 1] - 1;
else
{
for(int i = 2; i < n; i += 2)
ans ^= a[i] - a[i - 1] - 1;
ans ^= a[0] - 1;
}
if(ans)
printf("Georgia will win\n");
else
printf("Bob will win\n");
}
return 0;
}