• Unusual Matrix


    Unusual Matrix

    You are given two binary square matrices a and b of size n×n. A matrix is called binary if each of its elements is equal to 0 or 1. You can do the following operations on the matrix a arbitrary number of times (0 or more):

    • vertical xor. You choose the number j (1≤j≤n) and for all i (1≤i≤n) do the following: (a_{i,j}:=ai,j⊕1) (⊕ — is the operation xor (exclusive or)).
    • horizontal xor. You choose the number i (1≤i≤n) and for all j (1≤j≤n) do the following: (ai,j:=ai,j⊕1).

    Note that the elements of the a matrix change after each operation.

    For example, if n=3 and the matrix a is:

    [egin{pmatrix}1&1&0\0&0&1\1&1&0end{pmatrix} ]

    Then the following sequence of operations shows an example of transformations:

    • vertical xor, j=1.

      [a=egin{pmatrix}0&1&0\1&0&1\0&1&0end{pmatrix} ]

    • horizontal xor,i=2.

      [a=egin{pmatrix}0&1&0\0&1&0\0&1&0end{pmatrix} ]

    • vertical xor, j=2.

      [a=egin{pmatrix}0&0&0\0&0&0\0&0&0end{pmatrix} ]

    Check if there is a sequence of operations such that the matrix a becomes equal to the matrix b.

    Input

    The first line contains one integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.

    The first line of each test case contains one integer n (1≤n≤1000) — the size of the matrices.

    The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix a.

    An empty line follows.

    The following n lines contain strings of length n, consisting of the characters '0' and '1' — the description of the matrix b.

    It is guaranteed that the sum of n over all test cases does not exceed 1000.

    Output

    For each test case, output on a separate line:

    • "YES", there is such a sequence of operations that the matrix a becomes equal to the matrix b;
    • "NO" otherwise.

    Example

    input

    3
    3
    110
    001
    110
    
    000
    000
    000
    3
    101
    010
    101
    
    010
    101
    010
    2
    01
    11
    
    10
    10
    

    output

    YES
    YES
    NO
    

    Note

    The first test case is explained in the statements.

    In the second test case, the following sequence of operations is suitable:

    • horizontal xor, i=1;
    • horizontal xor, i=2;
    • horizontal xor, i=3;

    It can be proved that there is no sequence of operations in the third test case so that the matrix a becomes equal to the matrix b.

    设p[i]: 第i行异或次数, p[n+j]第j列异或次数

    显然(b[ i][j]=p[i]oplus p[n+j]oplus a[i][j])

    有2n个变量,可以列出n*n个等式

    可以发现,如果确定了(p[n+n]),则可以确定其余所有的p[i]

    不过只满足其中2n-1个等式

    (p[i]=a[i][n]oplus b[i][n]oplus p[n+n])

    (p[n+j]=a[n][n]oplus b[n][n]oplus a[n][j]oplus b[n][j]oplus p[n+n])

    最后验证所有的等式,如果p[n+n]修改次数超过1次,则无解

    int n,m,k;
    char a[MAXN][MAXN],b[MAXN][MAXN];
    int p[MAXN*2];
    int main(){
        int t;read(t);
        while(t--){
            read(n);
            for(int i=1;i<=n;++i)read(a[i]+1);
            for(int i=1;i<=n;++i)read(b[i]+1);
            for(int i=1;i<=n;++i){
                p[i]=(a[i][n]-48)^(b[i][n]-48);
            }
            for(int j=1;j<=n;++j){
                p[n+j]=(a[n][j]-48)^(b[n][j]-48)^(a[n][n]-48)^(b[n][n]-48);
            }
            int f=0,chd=0;
            for(int i=1;i<=n;++i){
                for(int j=1;j<=n;++j){
                    int k1=p[i]^p[n+j];
                    int k2=(a[i][j]-48)^(b[i][j]-48);
                    if(k1^f!=k2){
                        f^=1;
                        chd++;
                    }
                }
            }
            if(chd<=1){
                puts("YES");
            }else{
                puts("NO");
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Django(一)创建第一个Django的demo
    使用webdriver扒取网站小说(二)-----进阶篇(分层数据驱动)
    【求解答】在eclipse中运行Android项目出现的问题 ——Launching MyFirstAPP' has encountered a program. Errors occurred during the build.
    今天想写一点简单的东西-关于计算机算法的
    新的朋友
    Java基础回顾(3)
    java基础回顾(2)
    java基础回顾(一)
    Asp.Net Mvc AutoFac 的简单使用
    微信文件记录删除
  • 原文地址:https://www.cnblogs.com/foursmonth/p/14332059.html
Copyright © 2020-2023  润新知