Notice
Recent Posts
Recent Comments
Link
나의 개발일지
[백준] 7562번 나이트의 이동 [Python] 본문
- BFS
n = int(input())
for i in range(n):
l = int(input()) # 체스판 크기 ㅣ * ㅣ
now = list(map(int, input().split())) # 현재 위치
target = list(map(int, input().split())) # 목표 위치
visit = [[0] * l for _ in range(l)]
zx, zy = [-1, -2, -2, -1, 1, 2, 2, 1], [-2, -1, 1, 2, -2, -1, 1, 2]
Q = [now]
visit[now[0]][now[1]] = 1
while Q:
x, y = Q.pop(0)
if x == target[0] and y == target[1]:
print(visit[x][y] - 1)
break
for i in range(8):
nx, ny = x + zx[i], y + zy[i]
if 0 <= nx < l and 0 <= ny < l and visit[nx][ny] == 0:
visit[nx][ny] = visit[x][y] + 1
Q.append([nx, ny])
'백준' 카테고리의 다른 글
[백준] 7576번 토마토 [Python] (0) | 2023.07.31 |
---|---|
[백준] 1012번 유기농 배추 [Python] (0) | 2023.07.31 |
[백준] 1260번 DFS와 BFS [Python] (0) | 2023.07.31 |
[백준] 2606번 바이러스, 양방향 그래프 표현 (defaultdict) [Python] (0) | 2023.07.28 |
[Python] 정규식 (0) | 2023.06.14 |
Comments