-
코드
from collections import deque T = int(input()) dx = [-1, -2, -1, -2, 1, 2, 1, 2] dy = [2, 1, -2, -1, 2, 1, -2, -1] def knight(i, current_x, current_y, want_x, want_y): tree = list([0 for _ in range(i)] for _ in range(i)) que = deque([]) que.append([current_x,current_y]) tree[current_x][current_y] = 1 while que: a, b = que[0][0], que[0][1] que.popleft() if a == want_x and b == want_y: return tree[a][b]-1 for j in range(8): x = a + dx[j] y = b + dy[j] if 0 <= x < i and 0 <= y < i: if tree[x][y] == 0: que.append([x,y]) tree[x][y] = tree[a][b] +1 for j in range(i): print(*tree[j]) for _ in range(T): i = int(input()) current_x, current_y = map(int, input().split()) want_x, want_y = map(int, input().split()) answer = knight(i, current_x, current_y, want_x, want_y) print(answer)
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 1504번 특정한 최단 경로 (파이썬) (0) 2022.01.05 [백준] 1753번 최단경로 (파이썬) (0) 2022.01.05 [백준] 7576번 토마토 (파이썬) (0) 2022.01.05 [백준] 2178번 미로 탐색 (파이썬) (0) 2022.01.05 [백준] 1012번 유기농 배추 (파이썬) (0) 2022.01.04