1+ import sys
2+ from collections import deque
3+
4+ input = sys .stdin .readline
5+
6+ # 1. 나이트가 이동할 수 있는 8가지 방향을 정의
7+ directions = [(0 , 0 ), (- 1 , - 2 ), (1 , - 2 ), (- 2 , - 1 ), (2 , - 1 ), (- 1 , 2 ), (1 , 2 ), (- 2 , 1 ), (2 , 1 )]
8+
9+
10+ # 5. BFS 탐색 함수 정의
11+ def bfs (start_x , start_y ):
12+ # 시작 노드를 큐에 추가하고 해당 위치의 값을 0으로 설정
13+ queue = deque ([(start_x , start_y )])
14+ chess_map [start_x ][start_y ] = 0
15+
16+ # 큐가 빌 때까지 탐색
17+ while queue :
18+ x , y = queue .popleft ()
19+
20+ # 현재 위치에서 모든 방향으로 이동 시도
21+ for dx , dy in directions :
22+ nx , ny = x + dx , y + dy
23+
24+ # 5.2.1 체스판의 범위를 넘어가지 않는지 확인
25+ if 0 <= nx < l and 0 <= ny < l :
26+ # 아직 방문하지 않은 위치라면
27+ if chess_map [nx ][ny ] == - 1 :
28+ # 현재 위치에서 1 이동한 값으로 갱신하고 큐에 추가
29+ chess_map [nx ][ny ] = chess_map [x ][y ] + 1
30+ queue .append ((nx , ny ))
31+
32+ # 5.2.2. 목표 지점에 도달한 경우, 이동 횟수 반환
33+ if nx == fin_x and ny == fin_y :
34+ return chess_map [nx ][ny ]
35+
36+ return - 1 # 목표 지점에 도달하지 못한 경우
37+
38+
39+ # 2. 테스트 케이스 개수 입력
40+ t = int (input ())
41+ for _ in range (t ):
42+ # 3. 체스판의 크기, 시작 위치, 목표 위치 입력
43+ l = int (input ()) # 체스판의 한 변의 길이 l
44+ cur_x , cur_y = map (int , input ().split ()) # 시작 위치
45+ fin_x , fin_y = map (int , input ().split ()) # 목표 위치
46+
47+ # 4. 체스판 초기화 (-1로 초기화하여 미방문 표시)
48+ chess_map = [[- 1 ] * l for _ in range (l )]
49+
50+ # 6. BFS 탐색을 통해 결과 출력
51+ print (bfs (cur_x , cur_y ))
0 commit comments