Matrix Game(尼姆博弈)
description
given anm x nmatrix, wheremdenotes the number of rows andndenotes the number of columns and in each cell a pile of stones is given. for example, let there be a2 x 3matrix, 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 2ndrow 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 integert (≤ 100), denoting the number of test cases.
each case starts with a line containing two integers:mandn (1 ≤ m, n ≤ 50). each of the nextmlines containsnspace separated integers that form the matrix. all the integers will be between0and109(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
题目大意:
给定一个矩阵,然后两个人进行博弈(还是那两个人 alice 和bob),alice 先手,每次只能取每一行中的任意数量(必须是>=1的),最后取完的获胜。
解题思路:
这是一个简单的尼姆博弈,虽然加上了矩阵,但是也是没有什么难度,只要掌握了简单的尼姆博博弈这个题还是简单的,首先,我们这样想一下,将每一行当成尼姆博弈中的每一堆,这样就构造出来一个尼姆博弈的模型了就可以做了,只需要将每一行的元素加和,然后进行异或就行了。如果异或等于 0 那么后手赢,否则先手赢。
my code:
#include #include #include using namespace std; int main() { ///get_sg(); int t; scanf("%d",&t); for(int cas=1; cas<=t; cas++) { int m, n, x, ans=0; scanf("%d%d",&m,&n); for(int i=0; i