[알고리즘]백준 16948 데스나이트

CHOI IN HO·2024년 2월 19일
0

코딩테스트

목록 보기
53/74

풀이

크게 어려울 것 없이 기본적인 bfs방식의 풀이이다

from collections import deque
n = int(input())

r1, c1, r2, c2 = map(int, input().split())

dx =[-2,-2,0,0,2,2]
dy =[-1,1,-2,2,-1,1]

visited = []
ans = True
count = 0
def bfs(x,y,cnt):
    global ans, count
    q = deque()
    q.append((x,y,cnt))

    while q:
        x,y,cnt = q.popleft()
        if (x,y) not in visited:
            visited.append((x,y))
        else:continue
        if x == r2 and y == c2:
            ans = False
            count = cnt
            return

        for i in range(6):
            nx = x+dx[i]
            ny = y+dy[i]
            if nx < 0 or ny <0 or nx >= n or ny >= n:
                continue
            q.append((nx,ny, cnt+1))



bfs(r1,c1,0)
if ans == False:
    print(count)
else:print(-1)
profile
개발자기 되기 위해선 무엇이든!

0개의 댓글