https://www.acmicpc.net/problem/7562
https://github.com/stellaluminary/Baekjoon
백트래킹 외 가장 쉬운 방법은 초기 진입한 최초 횟수를 기록하면서 진행하는 BFS이다.
고로 본 방법은 BFS로 문제를 푼다.
from collections import deque
dm = [(-2,-1),(-1,-2),(1,-2),(2,-1),(2,1),(1,2),(-1,2),(-2,1)]
def bfs(current, target, cnt, visit):
q = deque([current])
visit[current[0]][current[1]] = 1
while q:
x, y = q.popleft()
if [x,y] == target:
return visit[x][y]
for i in range(8):
nx, ny = x+dm[i][0], y+dm[i][1]
if nx < 0 or ny < 0 or nx >= l or ny >= l:
continue
if not visit[nx][ny]:
visit[nx][ny] = visit[x][y] + 1
q.append([nx, ny])
for _ in range(int(input())):
l = int(input())
present = list(map(int, input().split()))
target = list(map(int, input().split()))
visit = [[0]*l for _ in range(l)]
val = bfs(present, target, 0, visit)
print(val-1)