# BOJ - 16509 - 장군

Updated:

``````import sys
from collections import deque

dx = [(-1, -2, -3), (-1, -2, -3), (0, -1, -2), (0, -1, -2), (0, 1, 2), (0, 1, 2), (1, 2, 3), (1, 2, 3)]
dy = [(0, -1, -2), (0, 1, 2), (-1, -2, -3), (1, 2, 3), (-1, -2, -3), (1, 2, 3), (0, -1, -2), (0, 1, 2)]

def moveCheck(i, x, y, ex, ey):
nx, ny = x, y
for j in range(3):
nx, ny = x + dx[i][j], y + dy[i][j]
if nx < 0 or nx >= 10 or ny < 0 or ny >= 9:
return -1, -1
if j < 2 and nx == ex and ny == ey:
return -1, -1
return nx, ny

def BFS(maps, sx, sy, ex, ey):
dq = deque()
dq.append((sx, sy))
maps[sx][sy] = 0

while dq:
x, y = dq.popleft()
if x == ex and y == ey:
return maps[x][y]
for i in range(8):
nx, ny = moveCheck(i, x, y, ex, ey)
if nx != -1 and maps[nx][ny] == -1:
dq.append((nx, ny))
maps[nx][ny] = maps[x][y] + 1
return -1

def solution():
sx, sy = map(int, sys.stdin.readline().split())
ex, ey = map(int, sys.stdin.readline().split())

maps = [[-1] * 9 for _ in range(10)]
print(BFS(maps, sx, sy, ex, ey))

solution()
``````

https://www.acmicpc.net/problem/16509

Categories:

Updated: