1 条题解

  • 0
    @ 2025-5-2 11:36:19
    #include <bits/stdc++.h>
    using namespace std;
    int n;
    int a[35][35];
    queue<pair<int, int>> q;
    int dx[] = {0, 0, 1, -1};
    int dy[] = {1, -1, 0, 0};
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n;
        // 边框(因为边框为 0,,所以不写也可以)
        for (int i = 0; i <= n + 1; i++)
            a[i][0] = a[0][i] = a[i][n + 1] = a[n + 1][i] = 0;
        // 原图
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                cin >> a[i][j];
        // 外面的 0 -> 3
        q.push(make_pair(0, 0));
        a[0][0] = 3;
        while (!q.empty())
        {
            pair<int, int> now = q.front();
            q.pop();
            for (int i = 0; i < 4; i++)
            {
                int nx = now.first + dx[i];
                int ny = now.second + dy[i];
                if (0 <= nx && nx <= n + 1 &&
                    0 <= ny && ny <= n + 1 &&
                    a[nx][ny] == 0)
                {
                    a[nx][ny] = 3;
                    q.push(make_pair(nx, ny));
                }
            }
        }
        // 0 -> 2
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (a[i][j] == 0)
                    a[i][j] = 2;
        // 3 -> 0
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (a[i][j] == 3)
                    a[i][j] = 0;
        // out
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
                cout << a[i][j] << " ";
            cout << "\n";
        }
    
        return 0;
    }
    

    信息

    ID
    1321
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    13
    已通过
    9
    上传者