Incredible Chess【 Nim博弈】
You are given an n x n chess board. Only pawn is used in the 'Incredible Chess' and they can move forward or backward. In each column there are two pawns, one white and one black. White pawns are placed in the lower part of the board and the black pawns are placed in the upper part of the board.
The game is played by two players. Initially a board configuration is given. One player uses white pieces while the other uses black. In each move, a player can move a pawn of his piece, which can go forward or backward any positive integer steps, but it cannot jump over any piece. White gives the first move.
The game ends when there is no move for a player and he will lose the game. Now you are given the initial configuration of the board. You have to write a program to determine who will be the winner.
Example of a Board
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case starts with an integer n (3 ≤ n ≤ 100) denoting the dimension of the board. The next line will contain n integers, W0, W1, ..., Wn-1 giving the position of the white pieces. The next line will also contain n integers, B0, B1, ... Bn-1 giving the position of the black pieces. Wi means the row position of the white piece of ith
Output
For each case, print the case number and 'white wins' or 'black wins' depending on the result.
Sample Input
2
6
1 3 2 2 0 1
5 5 5 3 1 2
7
1 3 2 2 0 4 0
3 4 4 3 1 5 6
Sample Output
Case 1: black wins
Case 2: white wins
分析:
把每一列的棋子間隔(可以走的)看成Nim博弈中的石子數量,將N堆石子數量異或,若結果為0,先手必敗;
#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
const int maxn=120;
int a[maxn],b[maxn];
int main(){
int T;
int ca=1;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int j=1;j<=n;j++)
scanf("%d",&b[j]);
int ans=0;
for(int i=1;i<=n;i++)
ans^=abs(b[i]-a[i]-1);
printf("Case %d: ",ca++);
if(ans==0)
printf("black wins\n");
else
printf("white wins\n");
}
return 0;
}