題目鏈接:http://poj.org/problem?id=1704
解題思路:
將兩個相鄰石子看成一組,每一組中前一個和後一個的距離等效成NIM博弈中石子個數(相鄰兩組之間距離無影響),這樣就實現了到NIM博弈的轉化,之後可以用NIM的異或規則進行博弈,需要注意的是石子不一定是偶數堆,如果是奇數堆則直接將第一組設爲最左邊一個元素,同時設其NIM值爲其可移動範圍即可。
示例代碼:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1010;
int T, n, a[maxn], s;
int main() {
scanf("%d", &T);
while (T --) {
scanf("%d", &n);
for (int i = 0; i < n; i ++) scanf("%d", a+i);
sort(a, a+n);
s = (n % 2) ? a[0] - 1 : 0;
for (int i = n%2; i < n; i += 2)
s ^= a[i+1] - a[i] - 1;
puts(s ? "Georgia will win" : "Bob will win");
}
return 0;
}