https://www.acmicpc.net/problem/7562
from collections import deque
# (x1, y1)에서 (x2, y2)까지 최소 몇 번 만에 이동하나?
def bfs(x1, y1, x2, y2, graph):
queue = deque()
visited = [[0]*len(graph) for _ in range(len(graph))]
queue.append((x1, y1))
visited[x1][y1] = 1
while queue:
v_x, v_y = queue.popleft()
if v_x == x2 and v_y == y2:
print(visited[v_x][v_y] - 1)
break
# 이동한 자리에 몇칸째 이동인지 기록하기
x_movs = [-1, -2, -2, -1, 1, 2, 2, 1]
y_movs = [-2, -1, 1, 2, -2, -1, 1, 2]
for i in range(8):
n_x = v_x + x_movs[i]
n_y = v_y + y_movs[i]
# 유효 범위
if 0<= n_x < len(graph) and 0 <= n_y < len(graph):
if visited[n_x][n_y] == 0:
visited[n_x][n_y] += visited[v_x][v_y] + 1
queue.append((n_x, n_y))
t = int(input())
for _ in range(t):
l = int(input())
chess = [[0]*l for __ in range(l)]
# 현재 있는 칸
current = tuple(map(int, input().split()))
# 이동하려는 칸
goal = tuple(map(int, input().split()))
bfs(current[0], current[1], goal[0], goal[1], chess)