其他分享
首页 > 其他分享> > [CF1475F] Unusual Matrix

[CF1475F] Unusual Matrix

作者:互联网

[CF1475F] Unusual Matrix

Description

对 \(n \times n\) 的 01 矩阵,每次操作可以反转一行或者一列。给定 \(a,b\) 两个矩阵,问 \(a\) 是否可以经过有限次操作变为 \(b\)。

Solution

只要我们确定了某一行(列)的操作情况,所有的操作情况都会被确定,因此只需枚举第一行是否操作,后面的递推计算即可,如果算下来还有剩余,那么就无解。

#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main()
{
    ios::sync_with_stdio(false);

    int t;
    cin >> t;

    while (t--)
    {
        int n;
        cin >> n;

        vector<vector<int>> a(n + 2, vector<int>(n + 2));
        vector<vector<int>> b(n + 2, vector<int>(n + 2));

        for (int i = 1; i <= n; i++)
        {
            string str;
            cin >> str;
            for (int j = 1; j <= n; j++)
                a[i][j] = str[j - 1] == '1';
        }

        for (int i = 1; i <= n; i++)
        {
            string str;
            cin >> str;
            for (int j = 1; j <= n; j++)
                b[i][j] = str[j - 1] == '1';
        }

        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                a[i][j] ^= b[i][j];
            }
        }

        auto t = a;

        for (int i = 2; i <= n; i++)
        {
            if (t[i][1])
            {
                for (int j = 1; j <= n; j++)
                    t[i][j] ^= 1;
            }
        }
        for (int j = 1; j <= n; j++)
        {
            if (t[1][j])
            {
                for (int i = 1; i <= n; i++)
                    t[i][j] ^= 1;
            }
        }

        int cnt = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                cnt += t[i][j];

        if (cnt == 0)
        {
            cout << "YES" << endl;
        }
        else
        {
            auto t = a;
            for (int j = 1; j <= n; j++)
                t[1][j] ^= 1;
            for (int i = 2; i <= n; i++)
            {
                if (t[i][1])
                {
                    for (int j = 1; j <= n; j++)
                        t[i][j] ^= 1;
                }
            }
            for (int j = 1; j <= n; j++)
            {
                if (t[1][j])
                {
                    for (int i = 1; i <= n; i++)
                        t[i][j] ^= 1;
                }
            }

            int cnt = 0;
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= n; j++)
                    cnt += t[i][j];

            if (cnt == 0)
            {
                cout << "YES" << endl;
            }
            else
            {
                cout << "NO" << endl;
            }
        }
    }
}

标签:Unusual,Matrix,int,long,CF1475F,vector,操作
来源: https://www.cnblogs.com/mollnn/p/14337899.html