#include <iostream>
#include <string>
#include <queue>
#include <algorithm>

using namespace std;
#define X first
#define Y second


int dx[8]={1,2,2,1,-1,-2,-2,-1};
int dy[8]={2,1,-1,-2,2,1,-1,-2};

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--) {
        int dist[302][302];
        queue<pair<int,int>> q;
        int n,x,y,x1,y1;
        cin >> n >> x >> y >> x1 >> y1;
        for(int i=0; i<n; i++) fill(dist[i],dist[i]+n,-1);
        dist[x][y]=0;
        q.push({x,y});
        while(!q.empty()){
            pair<int,int> cur=q.front();
            q.pop();
            if(cur.X==x1 && cur.Y==y1) break;
            for(int i=0; i<8; i++) {
                int nx=cur.X+dx[i];
                int ny=cur.Y+dy[i];
                if(nx<0 || nx>= n || ny <0 || ny >= n) continue;
                if(dist[nx][ny]!=-1) continue;

                dist[nx][ny]=dist[cur.X][cur.Y]+1;
                q.push({nx,ny});
            }
            
        }
        cout << dist[x1][y1] << '\n';
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: