题目链接:http://lightoj.com/volume_showproblem.php?problem=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
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 109 (inclusive).
Output
For each case, print the case number and 'Alice' if Alice wins, or 'Bob' otherwise.
Sample Input
2
2 3
2 3 8
5 2 7
2 3
1 2 3
3 2 1
Sample Output
Case 1: Alice
Case 2: Bob
题目翻译:
给定m x n矩阵,其中m表示行数,n 表示列数,在每个单元格中给出一堆石头。例如,让有一个2 x 3矩阵,并且堆是
2 3 8
5 2 7
这意味着在单元(1,1)中有一堆2块石头,在单元(1,2)中有一堆3块石头,等等。
现在爱丽丝和鲍勃正在这个矩阵里玩一个奇怪的游戏。爱丽丝先开始,他们轮流转。在每个回合中,玩家选择一行,并从该行中的任意数量的单元格中绘制任意数量的石头。但他/她必须画至少一块石头。例如,如果 Alice 在给定矩阵中选择第二行,她可以从单元格(2、1)、单元格 (2、 2) 中的 0 个宝石(2、3)中选取 2 个宝石。或者,她可以从细胞(2,1),细胞(2,2),细胞(2,3)中取5块石头。4块石头。有很多其他的方法,但她必须从所有桩至少挑一块石头。不能拿石头的玩家会输。
现在,如果两人都发挥最佳,谁会赢?
输入
输入以整数T(=100)开头,表示测试用例的数量。
每个案例以包含两个整数的行开头:m 和n(1 = m,n = 50)。下一个m线包含构成矩阵的 n 个空格分隔的整数。所有整数将介于0和10 9(包括)。
输出
对于每个案例,打印案例编号和"Alice"(如果 Alice 获胜)或"Bob"否则。
尼姆博弈变形题,只需要把每一行的所有石头加在一起,最后对每一行的和进行抑或,判断结果就行了。
#include<iostream>
using namespace std;
int a[1005][1005];
int main(){
int T;
scanf("%d",&T);
for(int kcase=1;kcase<=T;kcase++){
int n,m;
scanf("%d%d",&n,&m);
int sum=0;
for(int i=1;i<=n;++i){
int ans=0;
for(int j=1;j<=m;++j){
scanf("%d",&a[i][j]);
ans+=a[i][j];
}
sum^=ans;
}
if(sum) printf("Case %d: Alice\n",kcase);
else printf("Case %d: Bob\n",kcase);
}
return 0;
}