문제 링크: https://www.acmicpc.net/problem/16948
백준 알고리즘 중급 1/3 611에서 2번째 - 16948번 데스 나이트를 풀어보았다.
풀이: https://honggom.tistory.com/m/144
C++
Python
from collections import deque
def bfs(y, x):
q = deque()
q.append((y, x))
graph[y][x] = 0
while q:
y, x = q.popleft()
for dy, dx in d:
ny, nx = y+dy, x+dx
if 0 <= ny < n and 0 <= nx < n and graph[ny][nx] == -1:
q.append((ny, nx))
graph[ny][nx] = graph[y][x]+1
n = int(input())
r1, c1, r2, c2 = 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(r1, c1)
print(graph[r2][c2])
Java
'코테용 문제풀이 > 백준' 카테고리의 다른 글
연구소 풀이 (0) | 2023.02.23 |
---|---|
DSLR 풀이 (0) | 2023.02.22 |
뱀과 사다리 게임 풀이 (0) | 2023.02.22 |
2048 (Easy) 풀이 (0) | 2023.02.22 |
구슬 탈출 2 풀이 (0) | 2023.02.22 |