Agorithm/백준 알고리즘
백준 알고리즘 16948번 데스 나이트(python)
kimjinho1
2021. 3. 12. 14:46
728x90
반응형
기본적인 BFS 문제이다.
from collections import deque
def bfs(r, c):
q = deque()
q.append((r, c))
graph[r][c] = 0
while q:
r, c = q.popleft()
for dr, dc in d:
R, C = r+dr, c+dc
if (0 <= R < N) and (0 <= C < N) and graph[R][C] == -1:
q.append((R, C))
graph[R][C] = graph[r][c]+1
N = int(input())
sr, sc, er, ec = map(int, input().split())
graph = [[-1]*(N) for _ in range(N)]
d = [(-2, -1), (-2, 1), (0, -2), (0, 2), (2, -1), (2, 1)]
bfs(sr, sc)
print(graph[er][ec])
728x90
반응형