F. Unusual Matrix(Codeforces Round #697 (Div. 3)题解)
题目链接:F. Unusual Matrix
思路:我们可以发现,如果\(a_{i,j}\)不等于\(b_{i,j}\)那么他们变化的次数一定是奇数次,否则一定是偶数次,而该元素变化的总次数是行变化次数+列变化次数,所以我们通过\(a_{1,1}\)就能将1行和1列的情况枚举出来,进一步,因为我们知道其他元素的情况,所以我们就可以推出来其他行和其他列的变化次数的情况,然后再带入矩阵进行验证。
\(Code:\)
#include<set>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#define ch() getchar()
#define pc(x) putchar(x)
#include<stack>
#include<unordered_map>
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define ll long long
#define ull unsigned long long
#define pb emplace_back
#define mp make_pair
#define PI acos(-1)
using namespace std;
template<typename T>void read(T&x){
static char c;
static int f;
for(c=ch(),f=1; c<'0'||c>'9'; c=ch())if(c=='-')f=-f;
for(x=0; c>='0'&&c<='9'; c=ch())x=x*10+(c&15);
x*=f;
}
template<typename T>void write(T x){
static char q[65];
int cnt=0;
if(x<0)pc('-'),x=-x;
q[++cnt]=x%10,x/=10;
while(x)
q[++cnt]=x%10,x/=10;
while(cnt)pc(q[cnt--]+'0');
}
const int N = 1e3+10;
int _,n;
char a[N][N],b[N][N];
int row[N],col[N];
bool check(int x,int y){
row[1] = x;
col[1] = y;
rep(i,2,n){
if(b[1][i] == a[1][i])row[i] = col[1];
else row[i] = !col[1];
}
rep(i,2,n){
if(b[i][1] == a[i][1])col[i] = row[1];
else col[i] = !row[1];
}
rep(i,1,n){
rep(j,1,n){
if(b[i][j] == a[i][j] and col[i]!=row[j])return false;
if(b[i][j] != a[i][j] and col[i]==row[j])return false;
}
}
return true;
}
void solve(){
read(_);
while(_--){
read(n);
rep(i,1,n)scanf("%s",a[i]+1);
rep(i,1,n)scanf("%s",b[i]+1);
//rep(i,1,n)puts(b[i]+1);
bool flag = false;
if(b[1][1] == a[1][1]){
if(check(1,1) or check(0,0))flag = true;
}
else if(check(1,0) or check(0,1))flag = true;
if(flag)puts("YES");
else puts("NO");
}
}
signed main(){solve();return 0; }