728x90
반응형
기본적인 BFS 문제이다.
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:
Y, X = y+dy, x+dx
if (0 <= Y < 8) and (0 <= X < 8) and graph[Y][X] == -1:
q.append((Y, X))
graph[Y][X] = graph[y][x]+1
sy, sx = map(int, input().split())
ey, ex = map(int, input().split())
graph = [[-1]*8 for _ in range(8)]
d = [(-2, -1), (-2, 1), (2, -1), (2, 1), (-1, -2), (1, -2), (-1, 2), (1, 2)]
bfs(sy-1, sx-1)
print(graph[ey-1][ex-1])
728x90
반응형
'Agorithm > 백준 알고리즘' 카테고리의 다른 글
백준 알고리즘 13777번 Hunt The Rabbit(python) (0) | 2021.03.18 |
---|---|
백준 알고리즘 11448번 Ga(python) (0) | 2021.03.18 |
백준 알고리즘 6229번 Bronze Lilypad Pond(python) (0) | 2021.03.18 |
백준 알고리즘 6004번 The Chivalrous Cow(python) (0) | 2021.03.18 |
백준 알고리즘 17266번 어두운 굴다리(python) (0) | 2021.03.18 |