ν°μ€ν 리 λ·°
728x90
λ°μν
λ¬Έμ
λ¬Έμ νμ΄
N * N 체μ€ν λ΄μ λ°μ€ λμ΄νΈκ° r1, c1μ μμ λ, r2, c2μ λ°©λ¬Έκ°λ₯ νμ§λ₯Ό νλ¨νλ λ¬Έμ μ΄λ€. λ°μ€ λμ΄νΈλ λ¬Έμ μ μ£Όμ΄μ§ κ²κ³Ό κ°μ΄ 6κ°μ§ λ°©ν₯μΌλ‘ μμ§μΌ μ μλ€. λ°λΌμ ν μ μ μΌλ‘ λΆν° λ°©λ¬Έν μ μλ λͺ¨λ κ²½μ°μ μλ μλ νμ 1λ‘ μκ°νμ¬μΌ νλ€. λ°μ€ λμ΄νΈκ° λ°©λ¬Έν μ μλ λͺ¨λ μ μ μ λ€ λ°©λ¬Ένμ¬λ, r2, c2μ λλ¬νμ§ λͺ»ν κ²½μ°λ λ΅μ μ°Ύμ μ μλ κ²½μ°λ€.
μ½λ
from sys import stdin
from collections import deque
def visitable(x, y, visited):
return 0 <= x < n and 0 <= y < n and not visited[x][y]
def bfs(start):
q = deque([start])
visited = [[False] * n for _ in range(n)]
answer = 0
dirs = ((-2, -1), (-2, 1), (0, -2), (0, 2), (2, -1), (2, 1))
while q:
answer += 1
for _ in range(len(q)):
cur_x, cur_y = q.popleft()
for x, y in dirs:
next_x, next_y = cur_x + x, cur_y + y
if [next_x, next_y] == end:
return answer
if visitable(next_x, next_y, visited):
visited[next_x][next_y] = True
q.append([next_x, next_y])
return -1
if __name__ == '__main__':
n = int(stdin.readline())
locations = list(map(int, stdin.readline().split()))
start, end = locations[:2], locations[2:]
print(bfs(start))
728x90
λ°μν
'π¨βπ» μ½λ©ν μ€νΈ > λ°±μ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
λ°±μ€: 2206 λ²½ λΆμκ³ μ΄λνκΈ° (0) | 2020.08.03 |
---|---|
λ°±μ€: 14502 μ°κ΅¬μ (0) | 2020.08.03 |
λ°±μ€: 9663 N-Queen (0) | 2020.08.01 |
λ°±μ€: 14225 λΆλΆμμ΄μ ν© (0) | 2020.07.31 |
λ°±μ€: 2250 νΈλ¦¬μ λμ΄μ λλΉ (0) | 2020.07.29 |
λκΈ
κΈ λ³΄κ΄ν¨
μ΅κ·Όμ μ¬λΌμ¨ κΈ
μ΅κ·Όμ λ¬λ¦° λκΈ