Development/Algorithm & Coding Test
[파이썬, Python] 백준 16948번 : 데스 나이트
컴공 K
2022. 8. 8. 00:01
728x90
백준 16948번 : 데스 나이트

내 코드
import sys
from collections import deque
input = sys.stdin.readline
n = int(input())
r1, c1, r2, c2 = map(int, input().split())
cnt = 0
arr = [[-1] * n for _ in range(n)] # -1로 초기화
arr[r1][c1] = 0 # 시작 지점을 0으로 세팅
queue = deque([(r1, c1)])
directions = [(-2, -1), (-2, 1), (0, -2), (0, 2), (2, -1), (2, 1)]
while queue: # bfs 수행
r, c = queue.popleft()
for rr, cc in directions:
x, y = r + rr, c + cc
if 0 <= x < n and 0 <= y < n and arr[x][y] == -1:
arr[x][y] = arr[r][c] + 1
queue.append((x, y))
print(arr[r2][c2])
728x90
반응형