#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 (dist[x1][y1] == -1) {
            pair<int, int> cur = q.front();
            q.pop();
            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';
    }
    return 0;
}

Embed on website

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