Incredible Chess(LightOJ-1186)
题面
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.
输入
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 column. And Bi means the row position of the black piece of ith column. You can assume that (0 ≤ Wi < Bi < n) for (0 ≤ i < n) and at least one move is remaining.
输出
For each case, print the case number and ‘white wins’ or ‘black wins’ depending on the result.
样例输入
12
26
31 3 2 2 0 1
45 5 5 3 1 2
57
61 3 2 2 0 4 0
73 4 4 3 1 5 6
样例输出
1Case 1: black wins
2Case 2: white wins
提示
无
思路
Nim博弈,以黑白棋子间距为石子堆数做一个NIm博弈即可。
代码
1using namespace std;
2const int mxn = 1000;
3int a[mxn];
4
5int main()
6{
7 int T; scanf("%d", &T);
8 for(int cs=1; cs<=T; cs++)
9 {
10 int n; scanf("%d", &n);
11 for(int i=0; i<n; i++)
12 scanf("%d", &a[i]);
13
14 int nim = 0;
15 for(int i=0; i<n; i++) {
16 int x; scanf("%d", &x);
17 nim ^= x-a[i]-1;
18 }
19 printf("Case %d: ", cs);
20 if(nim){
21 printf("white wins\n");
22 }else{
23 printf("black wins\n");
24 }
25
26 }
27 return 0;
28}