zoukankan      html  css  js  c++  java
  • [CF1475F] Unusual Matrix

    [CF1475F] Unusual Matrix

    Description

    (n imes 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;
                }
            }
        }
    }
    
  • 相关阅读:
    第27章 结构型模式大PK
    第26章 创建型模式大PK
    Linux下C/C++代码调用PHP代码(转)
    php 5.2.17 升级到5.3.29
    PHP安全之register_globals (转)
    注意字符串的strlen与sizeof的差别
    嵌入式串口传送汉字乱码
    字符设备驱动模块与测试代码编写(转)
    字符设备之register_chrdev与register_chrdev_region(转)
    I2C子系统驱动框架及应用 (转)
  • 原文地址:https://www.cnblogs.com/mollnn/p/14337899.html
Copyright © 2011-2022 走看看