1 条题解

  • 0
    @ 2025-5-2 11:36:19

    能走到边缘的都是在包围圈外面的,所以以边缘作为起点能搜到的就是包围圈里面的

    用边缘作为起点

    #include <bits/stdc++.h>
    using namespace std;
    int n;
    int g[35][35];
    queue<pair<int, int>> q;
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                cin >> g[i][j];
        // 起点入队
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (g[i][j] == 0 &&
                    (i == 1 || i == n || j == 1 || j == n))
                {
                    q.push({i, j});
                    g[i][j] = 3;
                }
        // 只要队列非空就继续搜
        while (!q.empty())
        {
            // 取出队头
            pair<int, int> now = q.front();
            q.pop();
            // 考虑队头能扩展的位置
            for (int i = 0; i <= 3; i++)
            {
                int nx = now.first + dx[i];
                int ny = now.second + dy[i];
                // 只要能走到且没走过
                if (1 <= nx && nx <= n && 1 <= ny && ny <= n && g[nx][ny] == 0)
                {
                    q.push({nx, ny});
                    g[nx][ny] = 3;
                }
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
                if (g[i][j] == 3)
                    cout << 0 << " ";
                else if (g[i][j] == 0)
                    cout << 2 << " ";
                else
                    cout << g[i][j] << " ";
            cout << "\n";
        }
        return 0;
    }
    
    

    扩展第 0 行和第 n+1 行,把边缘连通起来

    #include <bits/stdc++.h>
    using namespace std;
    int n;
    int g[35][35];
    queue<pair<int, int>> q;
    int dx[4] = {0, 0, 1, -1};
    int dy[4] = {1, -1, 0, 0};
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                cin >> g[i][j];
        // 起点入队
        q.push({0, 0});
        g[0][0] = 3;
        // 只要队列非空就继续搜
        while (!q.empty())
        {
            // 取出队头
            pair<int, int> now = q.front();
            q.pop();
            // 考虑队头能扩展的位置
            for (int i = 0; i <= 3; i++)
            {
                int nx = now.first + dx[i];
                int ny = now.second + dy[i];
                // 只要能走到且没走过
                if (0 <= nx && nx <= n + 1 && 0 <= ny && ny <= n + 1 && g[nx][ny] == 0)
                {
                    q.push({nx, ny});
                    g[nx][ny] = 3;
                }
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
                if (g[i][j] == 3)
                    cout << 0 << " ";
                else if (g[i][j] == 0)
                    cout << 2 << " ";
                else
                    cout << g[i][j] << " ";
            cout << "\n";
        }
        return 0;
    }
    

    真的修改原地图

    #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;
    }
    
    • 1

    信息

    ID
    1321
    时间
    1000ms
    内存
    256MiB
    难度
    5
    标签
    递交数
    30
    已通过
    15
    上传者