Fliping game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1647 Accepted Submission(s): 1062
Problem Description
Alice and Bob are playing a kind of special game on an N*M board (N rows, M columns). At the beginning, there are N*M coins in this board with one in each grid and every coin may be upward or downward freely. Then they take
turns to choose a rectangle (x1, y1)-(n, m) (1 ≤ x1≤n, 1≤y1≤m) and flips all the coins (upward to downward, downward to upward) in it (i.e. flip all positions (x, y) where x1≤x≤n, y1≤y≤m)).
The only restriction is that the top-left corner (i.e. (x1, y1)) must be changing from upward to downward. The game ends when all coins are downward, and the one who cannot play in his (her) turns loses the game. Here's the problem: Who
will win the game if both use the best strategy? You can assume that Alice always goes first.
Input
The first line of the date is an integer T, which is the number of the text cases.
Then T cases follow, each case starts with two integers N and M indicate the size of the board. Then goes N line, each line with M integers shows the state of each coin, 1<=N,M<=100. 0 means that this coin is downward in the initial, 1 means that this coin is upward in the initial.
Then T cases follow, each case starts with two integers N and M indicate the size of the board. Then goes N line, each line with M integers shows the state of each coin, 1<=N,M<=100. 0 means that this coin is downward in the initial, 1 means that this coin is upward in the initial.
Output
For each case, output the winner’s name, either Alice or Bob.
Sample Input
2 2 2 1 1 1 1 3 3 0 0 0 0 0 0 0 0 0
Sample Output
Alice Bob
重点在理解题意上面。
题目大意:Alice和Bob在玩一个游戏,给定一个成N*M的矩阵的硬币,让他们两个轮流选中一个坐标为(x,y)的硬币,然后反转 (x, y) - (n, m)矩阵内的所有硬币,直至N*M矩阵内的所有硬币变为反面。
思路:不需要考虑怎么反转,因为每次反转,n*m位置的硬币都会被反转一次,而且最后会被反转为反面。
所以只需要考虑n*m位置的硬币就可以了。
是1的时候,需要奇数次反转才能变为0,奇数次反转会由Alice进行,所以Alice赢。
是0的时候,需要偶数次反转才能变为0,偶数次反转会由Bob进行,所以Bob赢。
代码:
#include<iostream>
using namespace std;
int main()
{
int t,n,m,x;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>x;
if(x==1)
cout<<"Alice"<<endl;
else
cout<<"Bob"<<endl;
}
return 0;
}