1 条题解

  • 0
    @ 2023-8-31 15:30:46
    #include <bits/stdc++.h>
    using namespace std;
    int n, m, x, y;
    int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2};
    int dy[] = {-1, 1, -2, 2, -2, 2, -1, 1};
    int dis[405][405];
    queue<pair<int, int>> q;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> n >> m >> x >> y;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                dis[i][j] = -1;
        dis[x][y] = 0;
        q.push(make_pair(x, y));
        while (!q.empty())
        {
            pair<int, int> now = q.front();
            q.pop();
            for (int i = 0; i < 8; i++)
            {
                int nx = now.first + dx[i];
                int ny = now.second + dy[i];
                if (1 <= nx && nx <= n &&
                    1 <= ny && ny <= m &&
                    dis[nx][ny] == -1)
                {
                    dis[nx][ny] = dis[now.first][now.second] + 1;
                    q.push(make_pair(nx, ny));
                }
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
                cout << left << setw(5) << dis[i][j];
            cout << "\n";
        }
        return 0;
    }
    
    • 1

    信息

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