文件目录

#include <bits/stdc++.h>
using namespace std;
int t, n, sx, sy, ex, ey;
struct node
{
    int x, y, step;
};
int dir[8][2] = {1, 2, -1, 2, 1, -2, -1, -2, 2, 1, -2, 1, 2, -1, -2, -1};
int bfs(int x, int y)
{
    queue<node> q;
    q.push((node){x, y, 0});
    bool vis[305][305] = {};
    vis[x][y] = true;
    while (!q.empty())
    {
        node u = q.front();
        q.pop();
        if (u.x == ex && u.y == ey)
            return u.step;
        for (int i = 0; i < 8; i++)
        {
            int xx = u.x + dir[i][0];
            int yy = u.y + dir[i][1];
            if (xx >= 0 && yy >= 0 && xx < n && yy < n && vis[xx][yy] == false)
            {
                vis[xx][yy] = true;
                q.push((node){xx, yy, u.step + 1});
            }
        }
    }
    return -1;
}
int main()
{
    cin >> t;
    while (t--)
    {
        cin >> n;
        cin >> sx >> sy >> ex >> ey;
        if (sx == ex && sy == ey)
            cout << 0 << endl;
        else
            cout << bfs(sx, sy) << endl;
    }
}