말처럼 이동하는 부분과 변사이를 이동할때를 두개 다 넣어주되, k번을 넘지 않도록 해준다.
k번 넘은 여부는 이전에 벽 부수고 이동하기 처럼 visited배열을 3차원으로 만들어줘서 넘은 여부를 확인해준다.
3차원 배열 기억하기 -> [[[False] * (k+1) for in range(201)] for in range(201]
import sys
from collections import deque
k = int(input())
w, h = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(h)]
visited = [[[0] * (k+1) for _ in range(201)] for _ in range(201)]
# visited = [[False]* 201 for _ in range(201)]
visited[0][0][0] == True
dx = [-1,1,0,0]
dy = [0,0,-1,1]
hx = [2,2,-2,-2,1,1,-1,-1]
hy = [1,-1,1,-1,2,-2,2,-2]
def bfs(k, lst):
q = deque()
q.append((0,0,0,0))
while q:
x,y,count,horse = q.popleft()
if count > w*h:
return -1
if x == h-1 and y == w-1:
return count
if horse < k:
for i in range(8):
xx = x + hx[i]
yy = y + hy[i]
if xx < 0 or yy < 0 or xx >= h or yy >= w:
continue
if lst[xx][yy] != 1 and visited[xx][yy][horse+1] == False:
visited[xx][yy][horse+1] = True
q.append((xx,yy,count+1, horse+1))
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx < 0 or ny < 0 or nx >= h or ny >= w:
continue
if lst[nx][ny] == 0 and visited[nx][ny][horse] == False:
visited[nx][ny][horse] = True
q.append((nx,ny,count+1, horse))
return -1
print(bfs(k, lst))