首页 > 其他 > 详细

[CF1475F] Unusual Matrix

时间:2021-01-28 09:04:51      阅读:45      评论:0      收藏:0      [点我收藏+]

[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;
            }
        }
    }
}

[CF1475F] Unusual Matrix

原文:https://www.cnblogs.com/mollnn/p/14337899.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!