题目
给你一个01矩阵再给你一个目标01矩阵。
你可以进行任意次的整行异或或者整列异或。
问当前矩阵是否能通过这两种操作转换成目标矩阵。
题解思路
以前见过这种题,套路满满。
我们可以先使用列异或使得第一行符合全部条件,这样我们就只能使用行异或了,因为一旦使用列异或就会使得第一行不符合要求。
直接打暴力就行了。
套路题。
AC代码
#include <bits/stdc++.h>
//#include <unordered_map>
//priority_queue
#define PII pair<int,int>
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 200100;
int a[1010][1010] ;
int t[1010][1010] ;
void solve()
{
int n ;
cin >> n ;
for (int i = 1 ; i <= n ; i++ )
for (int j = 1 ; j <= n ; j++ )
a[i][j] = 0 , t[i][j] = 0 ;
for (int i = 1 ; i <= n ; i++ )
{
for (int j = 1 ; j <= n ; j++ )
{
char st ;
cin >> st ;
if ( st == '1')
a[i][j] = 1 ;
}
}
for (int i = 1 ; i <= n ; i++ )
{
for (int j = 1 ; j <= n ; j++ )
{
char st ;
cin >> st ;
if ( st == '1')
t[i][j] = 1 ;
}
}
for (int j = 1 ; j <= n ; j++ )
{
if ( a[1][j] != t[1][j] )
{
for (int i = 1 ; i <= n ; i++ )
{
a[i][j] ^= 1 ;
}
}
}
int falg = 1 ;
for (int i = 1 ; i <= n ; i++ )
{
int cnt = 0 ;
for (int j = 1 ; j <= n ; j++ )
{
if ( a[i][j] != t[i][j] )
cnt++;
}
if ( cnt == 0 || cnt == n )
continue;
falg = 0 ;
break;
}
if (falg)
cout << "YES\n" ;
else
cout << "NO\n" ;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int T ;
cin >> T ;
while (T--)
solve() ;
return 0 ;
}