https://www.acmicpc.net/problem/7562
실패이유
: 구현실패
import collections
def bfs(s_x, s_y, e_x, e_y):
d = [[-1] * length for _ in range(length)] # -1로 모두 초기화, -1은 방문하지 않음을 나타내기도 함
queue = collections.deque()
queue.append((s_x, s_y))
d[s_y][s_x] = 0
while queue:
x, y = queue.popleft()
if x == e_x and y == e_y:
return d[y][x]
for i in range(8):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < length and 0 <= ny < length:
if d[ny][nx] == -1:
queue.append((nx, ny))
d[ny][nx] = d[y][x] + 1 # 다음 이동 횟수 = 이전 이동 횟수 + 1
dx = [-2, -1, 1, 2, 2, 1, -1, -2]
dy = [-1, -2, -2, -1, 1, 2, 2, 1] # 나이트 이동 방향
t = int(input())
for _ in range(t):
length = int(input())
start_x, start_y = map(int, input().split())
end_x, end_y = map(int, input().split())
print(bfs(start_x, start_y, end_x, end_y))
몇 번 움직이면 도착할 수 있을까?
하는 문제들은BFS
로 풀자d[][]
는 움직인 횟수를 나타낸다.
- -1로 모두 초기화 ➔ 방문하지 않았음을 나타내기도 한다.
출처 : 코드플러스 - 알고리즘 기초 2/2 강의
https://code.plus/course/42
골라오신 그림 킹받네요... 왜지...