Matrix Game(尼姆博弈)

A - Matrix Game

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Submit Status

Description

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 nspace 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

题目大意:

给定一个矩阵,然后两个人进行博弈(还是那两个人 Alice 和Bob),Alice 先手,每次只能取每一行中的任意数量(必须是>=1的),最后取完的获胜。

解题思路:

这是一个简单的尼姆博弈,虽然加上了矩阵,但是也是没有什么难度,只要掌握了简单的尼姆博博弈这个题还是简单的,首先,我们这样想一下,将每一行当成尼姆博弈中的每一堆,这样就构造出来一个尼姆博弈的模型了就可以做了,只需要将每一行的元素加和,然后进行异或就行了。如果异或等于 0 那么后手赢,否则先手赢。

My Code:

#include <iostream>
#include <cstdio>
#include <cstring>
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<m; i++)
        {
            int sum = 0;
            for(int j=0; j<n; j++)
            {
                scanf("%d",&x);
                sum += x;
            }
            ans ^= sum;
        }
        if(!ans)
            printf("Case %d: Bob\n",cas);
        else
            printf("Case %d: Alice\n",cas);
    }
    return 0;
}
时间: 2024-11-08 00:00:03

Matrix Game(尼姆博弈)的相关文章

最强头脑决定战中的尼姆博弈

最近看了一个日本综艺——最强头脑王决定战,顿觉以前看过的国内烧脑节目,类似“一站到底”之流与之相比真是相形见绌.“一站”说到底只是比谁题库背得熟,而日本这个真的是记忆.计算.观察.推理等各种能力的综合考察,比赛选手们各显神通,观众如我看得也是如痴如醉. 其中比赛到第二轮时有一道题如下:有四堆不同色的棋子,每堆分别有3.5.6.7枚,由选手和电脑开始依次从其中取出一些棋子,每次只可取同一堆的任意枚,如此交替直至取完所有棋子,谁取到最后一枚棋子谁输. 当时水上小哥不假思索地取子很轻松就赢了,让评委们

hdu 1907 尼姆博弈

John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3745    Accepted Submission(s): 2116 Problem Description Little John is playing very funny game with his younger brother. There is one big bo

hdu 1850 Being a Good Boy in Spring Festival (尼姆博弈)

Being a Good Boy in Spring Festival Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4658    Accepted Submission(s): 2781 Problem Description 一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场悄悄给爸爸买

Being a Good Boy in Spring Festival(尼姆博弈)

Being a Good Boy in Spring Festival Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 51 Accepted Submission(s): 45   Problem Description 一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场悄悄给爸爸买个小礼物主动地

nyoj 取石子(三)(尼姆博弈的定理运用)

取石子(三) 时间限制:1000 ms |  内存限制:1000 KB 难度:6 描述 小王喜欢与同事玩一些小游戏,今天他们选择了玩取石子. 游戏规则如下:共有N堆石子,已知每堆中石子的数量,两个人轮流取子,每次只能选择N堆石子中的一堆,取一定数量的石子(最少取一个),取过子之后,还可以将该堆石子中剩下的任意多个石子中随意选取几个放到其它的任意一堆或几堆上.等哪个人无法取子时就表示此人输掉了游戏.注意,一堆石子没有子之后,就不能再往此处放石子了. 假设每次都是小王先取石子,并且游戏双方都绝对聪明

hdu 1849(Rabbit and Grass) 尼姆博弈

Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3864    Accepted Submission(s): 2919 Problem Description 大学时光是浪漫的,女生是浪漫的,圣诞更是浪漫的,但是Rabbit和Grass这两个大学女生在今年的圣诞节却表现得一点都不浪漫:不去逛商场,不去逛

HDU 4315 Climbing the Hill (尼姆博弈)

Climbing the Hill Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description Alice and Bob are playing a game called "Climbing the Hill". The game board consists of cells arranged vertically, as the

HDU5014 Game(尼姆博弈)

Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 493    Accepted Submission(s): 380 Problem Description Here is a game for two players. The rule of the game is described below: ● In the beg

HDU 2176 取(m堆)石子游戏 尼姆博弈

题目思路: 对于尼姆博弈我们知道:op=a[1]^a[2]--a[n],若op==0先手必败 一个简单的数学公式:若op=a^b 那么:op^b=a: 对于第i堆a[i],op^a[i]的值代表其余各个堆值的亦或值. 我们现在希望将a[i]改变成某个更小的值使得,op^a[i]=0,反过来a[i]=op^0,输出它就好了 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h&