1 条题解

  • 0
    @ 2024-7-25 13:36:07

    参考答案:

    #include<iostream>
    #include<queue>
    #include<cstring>
    
    using namespace std;
    
    typedef pair<int, int>PII;
    const int N = 1010;
    int g[N][N], f[N][N];
    int n, m;
    
    void bfs(int a, int b)
    {
        queue<PII> q;
        q.push({ a,b });
        g[a][b] = 1;
    
        while (q.size())
        {
            PII t = q.front();
            q.pop();
            int dx[4] = { 0,0,-1,1 }, dy[4] = { 1,-1,0,0 };
            for (int i = 0; i < 4; ++i)
            {
                int x = t.first + dx[i], y = t.second + dy[i];
                if (g[x][y] == 0)
                {
                    g[x][y] = 1;
                    f[x][y] = f[t.first][t.second] + 1;
                    q.push({ x,y });
                }
            }
        }
    
        cout << f[n][m] << endl;
    }
    int main()
    {
        memset(g, 1, sizeof(g));
        cin >> n >> m;
    
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= m; ++j)
                cin >> g[i][j];
    
        bfs(1, 1);
    
        return 0;
    }
    
    • 1

    信息

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