题目大意:给定一个 (n*n) 的 (01) 矩阵,每次操作可以选择一行或者一列,整体 (xor) (1) ,求经过无限多次操作,能否变为目标矩阵
我们发现,每一行或每一列至多被操作一次,且如果某一列或某一列的操作确定,整个矩阵的操作就确定
所以枚举第一列是否被操作,获得整个矩阵的操作方法,再带回到矩阵中检验即可
代码:
#include<bits/stdc++.h>
#define rep(i,a,b) for (register int i=(a);i<=(b);i++)
#define drep(i,a,b) for (register int i=(a);i>=(b);i--)
typedef long long ll;
using namespace std;
inline ll read()
{
ll sum=0,f=0;char c=getchar();
while (!isdigit(c)) f|=(c=='-'),c=getchar();
while (isdigit(c)) sum=(sum<<1)+(sum<<3)+(c^48),c=getchar();
return f?-sum:sum;
}
const int N=1010;
int n,a[N][N],h[N],w[N];
char c1[N][N],c2[N][N];
inline bool chk1()
{
memset(h,0,sizeof(h)),memset(w,0,sizeof(w));
h[1]=1;
rep(i,1,n) if ((a[i][1]^h[1])==0) w[i]=0;else w[i]=1;
rep(i,2,n) if ((a[1][i]^w[1])==0) h[i]=0;else h[i]=1;
rep(i,1,n) rep(j,1,n) if ((a[i][j]^h[j]^w[i])==1) return false;
return true;
}
inline bool chk2()
{
memset(h,0,sizeof(h)),memset(w,0,sizeof(w));
h[1]=0;
rep(i,1,n) if ((a[i][1]^h[1])==0) w[i]=0;else w[i]=1;
rep(i,2,n) if ((a[1][i]^w[1])==0) h[i]=0;else h[i]=1;
rep(i,1,n) rep(j,1,n) if ((a[i][j]^h[j]^w[i])==1) return false;
return true;
}
inline void solve()
{
n=read();
rep(i,1,n) scanf("%s",c1[i]+1);
rep(i,1,n) scanf("%s",c2[i]+1);
rep(i,1,n) rep(j,1,n) a[i][j]=c1[i][j]^c2[i][j];
if (chk1()||chk2()) printf("YES
");
else printf("NO
");
}
int main()
{
drep(T,read(),1) solve();
}