ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[CF1475F] Unusual Matrix

2021-01-28 08:34:09  阅读:11  来源: 互联网

标签:Unusual Matrix int long CF1475F vector 操作


[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

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有