728x90
반응형
Python
import sys
from collections import deque
dx = [1,1,-1,-1,2,2,-2,-2]
dy = [2,-2,2,-2,1,-1,1,-1]
for _ in range(int(sys.stdin.readline().rstrip())):
l = int(sys.stdin.readline().rstrip())
board = [[0]*l for _ in range(l)]
now = list(map(int,sys.stdin.readline().split()))
dest = list(map(int,sys.stdin.readline().split()))
q = deque()
q.append(now)
while q:
pos = q.popleft()
if board[pos[0]][pos[1]] == 0:
board[pos[0]][pos[1]] = 1
for i in range(8):
nx = pos[0]+dx[i]
ny = pos[1]+dy[i]
if 0<=nx<l and 0<=ny<l:
if board[nx][ny]==0:
q.append([nx,ny])
board[nx][ny] = board[pos[0]][pos[1]]+1
print(board[dest[0]][dest[1]]-1)
728x90
반응형
'Algorithm Problems' 카테고리의 다른 글
[백준] [Python] 6603번 로또 - 백트래킹 (0) | 2021.05.11 |
---|---|
[백준] [Python] 2468번 안전지대 - BFS (0) | 2021.05.11 |
[백준] [USACO-Bronze] [Python] 적록색약 - BFS (0) | 2021.05.11 |
[백준] [Python] 15654번 N과 M (5) - 백트래킹 (0) | 2021.05.09 |
[백준] [Python] 11866번 요세푸스 문제 0 - 큐 (0) | 2021.05.08 |