Matrix Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 559 Accepted Submission(s): 282
Problem Description
At the start of the matrix game, we have an n * m chessboard in which each grid is painted alternatively in white or black. Every time, we can apply one of the two following operations:
Row flip operation: we can change the color of every grid in a single row.
Column swap operation: we can swap two columns (i.e., switch the colors between corresponding grids).
The task of the problem is, determine whether it’s possible to reach the target from the original chessboard by applying the two operations several times. Print ‘Yes’ or ‘No’ for each case.
Row flip operation: we can change the color of every grid in a single row.
Column swap operation: we can swap two columns (i.e., switch the colors between corresponding grids).
The task of the problem is, determine whether it’s possible to reach the target from the original chessboard by applying the two operations several times. Print ‘Yes’ or ‘No’ for each case.
Input
There are several test cases. For each case, there are two integers n and m in the first line (1 ≤ n, m ≤ 100), followed by two n * m 0/1 matrixes (0 stands for white color and 1 stands for black color) which are the original chessboard and the target chessboard respectively.
The input ends up with two negative numbers, which should not be processed as a case.
The input ends up with two negative numbers, which should not be processed as a case.
Output
For each test case, print ‘Yes’ or ‘No’ to tell whether it’s possible to reach the target.
Sample Input
2 2 1 1 1 0 0 0 0 1 2 2 1 1 1 0 0 0 0 0 -1 -1
Sample Output
Yes No
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int ma[105][105],tar[105][105],tmp[105][105];
int n,m;
void init()
{
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
scanf("%d",&ma[i][j]);
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
scanf("%d",&tar[i][j]);
}
}
void cpy(int c)
{
for (int i=1;i<=m;i++)
{
for (int j=1;j<=n;j++)
{
tmp[j][i]=ma[j][i];
}
}
}
void change_col(int a,int b)
{
for (int i=1;i<=n;i++)
{
int temp=tmp[i][a];
tmp[i][a]=tmp[i][b];
tmp[i][b]=temp;
}
}
void change_row(int c)
{
for (int i=1;i<=m;i++)
tmp[c][i]^=1;
}
bool ok(int a,int b)
{
for (int i=1;i<=n;i++)
{
if (tar[i][a]!=tmp[i][b])
return false;
}
return true;
}
int main()
{
while (scanf("%d%d",&n,&m))
{
if (n==-1) break;
init();
bool ans=0;
for (int i=1;i<=m;i++)
{
cpy(i);
change_col(1,i);
for (int j=1;j<=n;j++)
{
if (tmp[j][1]!=tar[j][1])
change_row(j);
}
for (int j=2;j<=m;j++)
{
for (int k=j;k<=m;k++)
{
ans=ok(j,k);
if (ans){
change_col(j,k);
break;
}
}
}
if (ans) break;
}
if (ans) puts("Yes");
else
puts("No");
}
return 0;
}