취준시절/백준

[백준 7562] 나이트의 이동 - Python

MAYMIN 2021. 7. 1. 01:22
728x90
SMALL

어제 풀다가 대체 왜 틀린지 모른상태로 오늘 밤부터 다시 풀었는데....

현타왔다..

 

dx = currentX + direction[i][0]
dy = currentY + direction[i][1]

 

이부분이 

 

dx = currentX = direction[i][0]
dy = currentY = direction[i][1]

 

이렇게 돼있었는데 그걸 발견을 못 했다.... 

ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ

진짜 어이없는 실수해서 시간 잡아먹을 때가 너무 속상하다 ㅠㅠ 😓

from collections import deque


testCase = int(input())

direction = [(-2,-1),(-1,-2),(1,-2),(2,-1),(-2,1),(-1,2),(1,2),(2,1)]

def bfs(nowX,nowY,nextX,nextY,idx):
    queue = deque()
    queue.append((nowX,nowY))
    result[nowX][nowY] = 1
    if nowX == nextX and nowY == nextY:
        answer[idx]=0
    while queue:
        currentX,currentY = queue.popleft()
        if currentX == nextX and currentY == nextY:
            answer[idx]=result[currentX][currentY]-1
            break

        for i in range(8):
            dx = currentX + direction[i][0]
            dy = currentY + direction[i][1]
            if 0<= dx <length and 0<=dy<length :
                if result[dx][dy] == 0:
                    queue.append((dx, dy))
                    result[dx][dy] = result[currentX][currentY]+1
answer = [0]*testCase
for i in range(testCase):
    length = int(input())
    now_x,now_y = map(int,input().split())
    next_x,next_y = map(int,input().split())
    result = [[0] * length for _ in range(length)]
    bfs(now_x,now_y,next_x,next_y,i)

for i in answer:
    print(i)
728x90
LIST