1 条题解

  • 0
    @ 2025-7-12 9:46:26

    基础做法

    #include <bits/stdc++.h>
    using namespace std;
    int t, n, m;
    char g[105][105];
    // 返回左上角为 (x,y) 的矩形是否符合要求
    bool check(int x, int y)
    {
        for (int i = x; i <= x + 3; i++)
            for (int j = y; j <= y + 3; j++)
            {
                // 应该是 0,但是是 1
                if ((i == x || i == x + 3 || j == y || j == y + 3) &&
                    g[i][j] == '1')
                    return false;
                // 应该是 1,但事实 0
                if ((x + 1 <= i && i <= x + 2) &&
                    (y + 1 <= j && j <= y + 2) &&
                    g[i][j] == '0')
                    return false;
            }
        return true;
    }
    void work()
    {
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                cin >> g[i][j];
        for (int x = 1; x + 3 <= n; x++)
        {
            for (int y = 1; y + 3 <= m; y++)
            {
                // 假设一个四行四列的左上角是 (x, y)
                // 那么右下角一定是 (x+3, y+3)
                if (check(x, y))
                {
                    cout << "Yes\n";
                    return;
                }
            }
        }
        cout << "No\n";
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> t;
        while (t--)
            work();
        return 0;
    }
    

    另一种简单的 check

    // 返回左上角为 (x,y) 的矩形是否符合要求
    bool check(int x, int y)
    {
        for (int i = x; i <= x + 3; i++)
            for (int j = y; j <= y + 3; j++)
                if (i == x || i == x + 3 || j == y || j == y + 3)
                {
                    // 应该是 0
                    if (g[i][j] == '1')
                        return false;
                }
                else
                {
                    // 应该是 1
                    if (g[i][j] == '0')
                        return false;
                }
        return true;
    }
    
    • 1

    信息

    ID
    12540
    时间
    1000ms
    内存
    512MiB
    难度
    2
    标签
    递交数
    10
    已通过
    6
    上传者