1 条题解

  • 0
    @ 2023-8-19 20:03:56
    #include <bits/stdc++.h>
    using namespace std;
    #define LL long long
    #define N 100010
    int n, sx, sy, ex, ey;
    int d[310][310];
    struct node
    {
        int x, y;
    };
    int dx[8] = {-2, -2, -1, 1, 2, 2, 1, -1};
    int dy[8] = {-1, 1, 2, 2, 1, -1, -2, -2};
    
    void solve()
    {
        queue<node> q;
        q.push({sx, sy});
        memset(d, -1, sizeof d);
        d[sx][sy] = 0;
        while (!q.empty())
        {
            node now = q.front();
            q.pop();
            for (int k = 0; k < 8; k++)
            {
                int nx = now.x + dx[k];
                int ny = now.y + dy[k];
                if (nx >= 0 && nx < n && ny >= 0 && ny < n && d[nx][ny] == -1)
                {
                    d[nx][ny] = d[now.x][now.y] + 1;
                    q.push({nx, ny});
                }
            }
        }
        cout << d[ex][ey] << '\n';
    }
    int main()
    {
        int T;
        cin >> T;
        while (T--)
        {
            cin >> n;
            cin >> sx >> sy;
            cin >> ex >> ey;
            solve();
        }
    }
    
    • 1

    信息

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