LightOJ 1247 Matrix Game (尼姆博弈)
2016-04-15 11:20
405 查看
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 containsn 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行 每行n个数 每次选择一行取任意个数 谁取到最后一个谁赢。
题解:最基本常规的尼姆博弈 因为每一行可以任意取 所以每一行看成一堆即可
将每一行进行异或 所得结果ans如果不为0先手赢 否则后手赢
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 containsn 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行 每行n个数 每次选择一行取任意个数 谁取到最后一个谁赢。
题解:最基本常规的尼姆博弈 因为每一行可以任意取 所以每一行看成一堆即可
将每一行进行异或 所得结果ans如果不为0先手赢 否则后手赢
#include <iostream> #include <cstdio> using namespace std; int main() { int t,cas=1; cin>>t; while(t--) { int m,n,data,ans=0; cin>>m>>n; for(int i=0;i<m;i++) { int sum=0; for(int j=0;j<n;j++) { cin>>data; sum+=data; } ans^=sum; } if(ans) printf("Case %d: Alice\n",cas++); else printf("Case %d: Bob\n",cas++); } return 0; }
相关文章推荐
- js 引号问题
- Ubuntu14.04安装配置Hadoop2.6.0(完全分布式)与 wordcount实例运行
- Linux基础知识汇总
- 欢迎使用CSDN-markdown编辑器
- 快速幂取模,POJ(1995)
- LeetCode(61)-Valid Palindrome
- linux内核部件分析之——设备驱动模型之class
- IOS 通用静态库、Makefile文件使用
- LeetCode(61)-Valid Palindrome
- LeetCode(61)-Valid Palindrome
- json解析和构建
- TCP/IP常见问题总结(三)
- HTML5移动网站制作教程
- pyqt4的qwebpage一个奇怪的bug
- java DateUtil
- ListView 各行变色写法
- LeetCode OJ 61. Rotate List
- 详解设计模式(四)
- 复杂sql (mysql)
- 科比退役后第一天:8点去自己公司上班