Game
Time Limit: 3000msMemory Limit: 131072KB 64-bit integer IO format: %lld Java class name: Main
Prev Submit Status Statistics Discuss Next
Bill is fond of computer games. He likes to analyze games and to provide efficient solutions. Now, he is studying the following game. The game starts with a n x n matrix filled with positive integers. When it is her/his turn, a player can delete the last row or the last column of the matrix, if the sum of the numbers in that row/column is even. If a player cannot delete the last row or the last column on his turn, then he loses the game. Bill thinks that this game can be classified as first player wins (W) or first player loses (L). First player wins means that the first player has a strategy to win, no matter how the second player plays the game. First player loses means that no matter what the first player does the second player has a strategy to win.
Bill is also a skilled programmer. He wants to write a program to classify the game quickly. Can you help him?
Input
The program input is from a text file. Each data set in the file stands for a particular game. A data set starts with the number n (n < 1000), the matrix dimension, followed by the positive integers in the matrix.
Output
The program has to print W' if the first player wins the game, or
L’ if the first player loses the game.
Notes:
White spaces can occur freely in the input. The input data are correct and terminate with an end of file. For each set of data the program prints the result to the standard output from the beginning of the line.
An input/output sample is in the table bellow. There are two data sets. In the first case, the matrix dimension n is 2. The integers in the matrix are: 2 4 6 8. The result for the data set is L, meaning that no matter what the first player does, the second player wins.
Sample Input
2
2 4
6 8
3
5 4 2
1 5 9
7 3 8
Sample Output
L
W
题目大意:给你一个矩阵,两个人轮流删除矩阵的最后一行或最后一列,但删除的那一行或那一列的和必须是偶数,输出第一个人的输赢状态。
思路:用DP记录当前矩阵的输赢状态
AC代码:
#include<cstdio>
#include<cstring>
int r[1010][1010];
int c[1010][1010];
int dp[1010][1010];//记录第一人在当前位置的输赢状态
int main()
{
int n,t;
while(scanf("%d",&n)!=EOF)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
scanf("%d",&t);
c[i][j]=(c[i][j-1]+t)%2;//记录i*j矩阵的最后一行的奇偶性
r[i][j]=(r[i-1][j]+t)%2;//记录i*j矩阵的最后一列的奇偶性
}
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
dp[i][j]=0;//初始化状态
if(!c[i][j]&&dp[i-1][j]==0)
dp[i][j]=1;//第一个人赢
if(!r[i][j]&&dp[i][j-1]==0)
dp[i][j]=1;
}
}
if(dp[n][n]) printf("W\n");
else printf("L\n");
}
return 0;
}