ν°μ€ν 리 λ·°
728x90
λ°μν
λ¬Έμ
λ¬Έμ νμ΄
μμ λ€λ£¬ 2178 λ―Έλ‘ νμκ³Ό λμΌν λ°©μμΌλ‘ νλ©΄ λλ€. μ°¨μ΄μ μ΄ μλ€λ©΄ νμνλ λ°©ν₯μ΄ λκ°μ 8 λ°©ν₯μ΄λΌλ κ²λΏμ΄λ€. λμ΄νΈκ° μ΄λν μ μλ κ²½λ‘λ‘ μ΄λν λ, νμ¬ κ²½λ‘κΉμ§ μ€λλ° μ΄λν νμλ₯Ό κ³μ λμ ν΄κ°λ©° μ΄λνλ©΄ λλ€. λ°λΌμ μνλ λμ°© μμΉμ λμ°©νμ λ μ€μ²©λ κ°μ΄ μ°Ύκ³ μ νλ λ΅μ΄λ€.
μ½λ
from sys import stdin
from collections import deque
def visitable(x, y):
return 0 <= x < l and 0 <= y < l and not graph[x][y]
def bfs(start, end):
q = deque([start])
while q:
if q[0] == end:
x, y = end
break
cur_x, cur_y = q.popleft()
for x, y in dirs:
next_x, next_y = x + cur_x, y + cur_y
if visitable(next_x, next_y):
graph[next_x][next_y] = graph[cur_x][cur_y] + 1
q.append([next_x, next_y])
return graph[x][y]
if __name__ == "__main__":
tc = int(stdin.readline())
for _ in range(tc):
l = int(stdin.readline())
start = list(map(int, stdin.readline().split()))
end = list(map(int, stdin.readline().split()))
dirs = ((-1, 2), (1, 2), (2, 1), (2, - 1),
(1, -2), (-1, -2), (-2, -1), (-2, 1))
graph = [[0] * l for _ in range(l)]
print(bfs(start, end))
728x90
λ°μν
'π¨βπ» μ½λ©ν μ€νΈ > λ°±μ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
λ°±μ€: 16929 Two Dots (0) | 2020.07.23 |
---|---|
λ°±μ€: 1707 μ΄λΆ κ·Έλν (0) | 2020.07.22 |
λ°±μ€: 7576 ν λ§ν (0) | 2020.07.22 |
λ°±μ€: 2178 λ―Έλ‘ νμ (0) | 2020.07.21 |
λ°±μ€: 4963 μ¬μ κ°μ (0) | 2020.07.21 |
λκΈ
κΈ λ³΄κ΄ν¨
μ΅κ·Όμ μ¬λΌμ¨ κΈ
μ΅κ·Όμ λ¬λ¦° λκΈ