Question E: Matrix conversion --------------------------------------------- Thinking (Routine Questions)

Title Description
Dongdong obtains two matrices A and B of size n rows and m columns, each of which contains only 0 and 1. Dongdong can perform the following operations on matrix A any number of times:
take any submatrix of matrix A with at least two rows and two columns, and invert the values ​​at its four corners (ie, 0 becomes 1, and 1 becomes 0 ).
Dongdong wants to know whether A can be converted into B.
Input The
test contains multiple sets of data. Enter a positive integer t (3≤t≤10) in the first line to indicate the number of test data sets.
The input of each set of test data is as follows: the
first line enters two positive integers n, m (2≤n, m≤500), which represents the number of rows and columns of the matrix.
Next, enter 2 * n lines with m 0s or 1s in each line, with no spaces in between.
Output
For each set of test data, if A can be transferred to B, then output Yes, otherwise output No.
Sample input Copy
3
3 3
010
010
100
100
100
100
3 4
0101
1010
0101
1111
1111
1111
6 7
0011001
0100101
0001001
1010100
0100101
0101001
1101011
0110100
1101001
1010010
0110100
0111101
Sample output Copy
Yes
No
Yes

Analysis:
Routine problem analysis parity.
Each time you modify a row, two numbers change, and a column also changes two numbers.
Pre-process all the modified positions a [i] [j] ^ b [i] [j] = 1 to
determine whether the number of 1 in each row and column is an odd number, output NO for odd numbers,
otherwise output YES

#include<bits/stdc++.h>
using namespace std;
const int N=1000;
int t,n,m;
char a[N][N];
char b[N][N];
int g[N][N];
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&n,&m);
		for(int i=1;i<=n;i++) cin>>(a[i]+1);
		for(int i=1;i<=n;i++) cin>>(b[i]+1);
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				g[i][j]=(a[i][j]-'0')^(b[i][j]-'0');
			}
		}
		int f=0;
		for(int i=1;i<=n;i++)
		{
			int num=0;
			for(int j=1;j<=m;j++)
			{
				if(g[i][j]==1) num++;
			 } 
			 if(num&1) f=1;
		}
		for(int i=1;i<=m;i++)
		{
			int num=0;
			for(int j=1;j<=n;j++)
			{
				if(g[j][i]==1) num++;
			 } 
			 if(num&1) f=1;
		}
		if(f==1) cout<<"No"<<endl;
		else cout<<"Yes"<<endl;
	}
}
Published 572 original articles · praised 14 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_43690454/article/details/105197755