A - Matrix Game(nim 博弈)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

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

简单的nim博弈,数组每一行加起来得一个sum,每个sum异或,最后得0后者胜。

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
int a[55][55];
int main()
{
  int t;
  cin>>t;
  for(int i=1;i<=t;i++)
  {
      int m,n;
      cin>>m>>n;
      int ans=0;
      for(int j=0;j<m;j++)
      {     int sum=0;
          for(int k=0;k<n;k++)
          {
              cin>>a[j][k];sum+=a[j][k];
          }
          ans^=sum;
      }
      if(ans!=0)
                printf("Case %d: Alice\n",i);
                else
                     printf("Case %d: Bob\n",i);
  }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值