Matrix Game (Nim博弈)
来源:互联网 发布:南通国税官网通知公告 编辑:程序博客网 时间:2024/06/16 12:09
Matrix Game (Nim博弈):http://acm.hust.edu.cn/vjudge/contest/view.action?cid=112620#problem/A 传送门:nefu
题面描述:
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 n 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列的石子,Alice和Bob依次进行取石子游戏,每一次都必须要在某一行中进行取石子,且取石子的个数要大于等于1个,先取完者先赢。
题目分析:
由于是在每一行中取石子,所以要把每一行的石子的个数加起来,再构造成一个新的堆,这样就一共会有m个堆,然后再按照正常的NIm博弈去求解问题,先取完先赢的状态,详细解释见Nim博弈的转载博客。
代码实现:
#include <iostream>using namespace std;int data[55];int main(){ int t,casenum=0; int m,n; long long sum; long long ans; cin>>t; while(t--) { cin>>m>>n; sum=0; ans=0; for(int i=0;i<m;i++) { sum=0; for(int j=0;j<n;j++) { cin>>data[j]; sum+=data[j]; } ans^=sum; } if(ans) cout<<"Case "<<++casenum<<": Alice"<<endl; else cout<<"Case "<<++casenum<<": Bob"<<endl; } return 0;}
- Matrix Game (Nim博弈)
- lightoj 1247 - Matrix Game Nim博弈
- A - Matrix Game(nim 博弈)LightOJ 1247
- HDU5011 Game(Nim博弈)
- HDU5011 Game(Nim博弈)
- Northcott Game (Nim博弈)
- HDU 5011 Game(Nim博弈)
- 2.Nim Game(尼姆博弈)
- hdu 1730 Northcott Game (Nim博弈)
- HDU 3389 Game(nim博弈)
- Northcott Game + nim博弈
- hdu3389 Game NIM博弈
- hdu1730Northcott Game NIM博弈
- leetcode---Nim Game---博弈
- Nim Game 博弈游戏
- Northcott Game + 博弈 + nim博弈
- poj 2315 Football Game (博弈 nim k game)
- Matrix Game(尼姆博弈)
- 台大林轩田机器学习课程笔记3----机器学习的可行性
- JAVA设计模式初探之桥接模式
- 【杭电】[5665]Lucky
- 软件开发工具整合
- 卡尔曼的原理
- Matrix Game (Nim博弈)
- String、StringBuffer与StringBuilder比较
- 401 Unauthorized: ERROR Failed to connect to newly launched supervisor. Agent will exit
- 算法导论 第22章 22.1-5
- Spring mvc学习笔记-学习思路
- java.lang.OutOfMemoryError异常解决方法
- linux下select使用--转载http://www.cnblogs.com/moonvan/archive/2012/05/26/2518881.html
- JavaScript之Unicode编码转换小工具
- LeetCode 47. Permutations II