📖 문제 링크
https://www.acmicpc.net/problem/7562
7562번: 나이트의 이동
체스판 위에 한 나이트가 놓여져 있다. 나이트가 한 번에 이동할 수 있는 칸은 아래 그림에 나와있다. 나이트가 이동하려고 하는 칸이 주어진다. 나이트는 몇 번 움직이면 이 칸으로 이동할 수
www.acmicpc.net
✅ 최종 코드
from collections import deque
def bfs():
while check:
x, y = check.popleft()
if x == xd and y == yd:
return board[x][y]
for i in range(len(dx)):
nx = x+dx[i]
ny = y+dy[i]
if 0<=nx<n and 0<=ny<n and board[nx][ny] == 0:
board[nx][ny] = board[x][y] + 1
check.append([nx, ny])
t = int(input())
dx = [-2, -2, -1, 1, 2, 2, 1, -1]
dy = [-1, 1, 2, 2, 1, -1, -2, -2]
for i in range(t):
n = int(input())
board = [[0]*n for _ in range(n)]
x, y = map(int, input().split())
xd, yd = map(int, input().split())
check = deque()
check.append([x, y])
print(bfs())
'알고리즘 > Python' 카테고리의 다른 글
[백준/Python] 21608번 : 상어 초등학교 (0) | 2022.04.27 |
---|---|
[백준/Python] 17144번: 미세먼지 안녕! (0) | 2022.04.21 |
[백준/Python] 7576번: 토마토 (JAVA버전 추가) (0) | 2022.04.13 |
[백준/Python] 14503번: 로봇청소기 (0) | 2022.04.03 |
[백준/Python] 14499번 : 주사위 굴리기 (0) | 2022.03.31 |