Matrix Game(LightOJ-1247)
题面
Given an m x n matrix, where m denotes the number of rows and n denotes the number of columns and in each cell a pile of stones is given. For example, let there be a 2 x 3 matrix, and the piles are
2 3 8
5 2 7
That means that in cell(1, 1) there is a pile with 2 stones, in cell(1, 2) there is a pile with 3 stones and so on.
Now Alice and Bob are playing a strange game in this matrix. Alice starts first and they alternate turns. In each turn a player selects a row, and can draw any number of stones from any number of cells in that row. But he/she must draw at least one stone. For example, if Alice chooses the 2nd row in the given matrix, she can pick 2 stones from cell(2, 1), 0 stones from cell (2, 2), 7 stones from cell(2, 3). Or she can pick 5 stones from cell(2, 1), 1 stone from cell(2, 2), 4 stones from cell(2, 3). There are many other ways but she must pick at least one stone from all piles. The player who can’t take any stones loses.
Now if both play optimally who will win?
输入
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with a line containing two integers: m and n (1 ≤ m, n ≤ 50). Each of the next m lines contains n space separated integers that form the matrix. All the integers will be between 0 and 10^9 (inclusive).
输出
For each case, print the case number and ‘Alice’ if Alice wins, or ‘Bob’ otherwise.
样例输入
12
22 3
32 3 8
45 2 7
52 3
61 2 3
73 2 1
样例输出
1Case 1: Alice
2Case 2: Bob
提示
无
思路
Nim博弈,每行任意取,以每行总和为石子堆数做一个NIm博弈即可。
代码
1using namespace std;
2
3int main()
4{
5 int T; scanf("%d", &T);
6 for(int cs=1; cs<=T; cs++)
7 {
8 int n, m;
9 scanf("%d %d", &n, &m);
10
11 int nim = 0;
12 for(int i=0; i<n; i++){
13 int sum = 0;
14 for(int j=0; j<m; j++){
15 int x; scanf("%d", &x);
16 sum += x;
17 }
18 nim ^= sum;
19 }
20 printf("Case %d: ", cs);
21 if(nim){
22 printf("Alice\n");
23 }else{
24 printf("Bob\n");
25 }
26
27 }
28 return 0;
29}