Matrix game (Nim game)

Source: Internet
Author: User

A- Matrix GameTime limit:$ MS Memory Limit:32768KB 64bit IO Format:%lld & Amp %llu Submit Status

Description

Given an m x n Matrix, where m denotes the number of rows and n denotes the number of columns a nd in each cell a pile of stones is given. For example, let there is a 2 x 3 matrix, and the piles are

2 3 8

5 2 7

That means this in cell (1, 1) There are a pile with 2 stones, in cell (1, 2) There are a pile with 3 stones and so on.

Now Alice and Bob is playing a strange game in the This matrix. Alice starts first and they alternate turns. In each turn a player selects a row, and can draw any number of the stones from the any number of the 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 the cell (2, 1), 1 stone from the cell (2, 2), 4 stones from cell (2, 3). There is many other ways and she must pick at least one stone from all piles. The player can ' t take any stones loses.

Now if both play optimally who would win?

Input

Input starts with an integer T (≤100), denoting the number of test cases.

Each case starts with a line containing the 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 would 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


Main topic:

Given a matrix, and then two people play games (or the two people Alice and Bob), Alice Initiator, can only fetch any number of each row (must be >=1), and finally finished winning.


Problem Solving Ideas:
This is a simple NIM game, although added to the matrix, but there is no difficulty, as long as the grasp of the simple Nimbo game is still simple, first of all, we think about each row as Nim game each heap, so that a model of the Nim game can be done, You only need to add the elements of each row and then make an XOR. If the XOR equals 0 then the flip win, otherwise the initiator wins.


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;}



Matrix game (Nim game)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.