λ¬Έμ
νλ‘κ·Έλλ¨Έμ€ - λΈλ‘ μ΄λνκΈ°
νμ΄ κ³Όμ
λ‘λ΄μ΄ μ΄λν μ μλ λͺ¨λ κ²½μ°λ₯Ό λ°μ Έλ³΄λ©° νμμ μννλ©΄ λλ€.
μ΅λ¨ κ²½λ‘λ₯Ό μ°ΎμΌλ©΄ λ°λ‘ νμμ μ’
λ£νλ©΄λλ―λ‘ κ°μ depthμ μνλ€μ νμνκΈ° μν΄ BFS
λ₯Ό μ¬μ©νλ€.
λ€λ§ λ‘λ΄μ μνμ νμ μ°μ°μ ꡬννλ λΆλΆμ΄ λ§€μ° κΉλ€λ‘μ λ€..
λ‘λ΄μ μν λνλ΄κΈ°
μ²μμλ board
μ λ‘λ΄μ μνλ₯Ό νμν΄μ£Όλ©° μ΄λ₯Ό νμμ νμ©νμμ§λ§ μΌλ§ μ§λμ§ μμ μ΄ λ°©λ²μ΄ κ΅μ₯ν λΉν¨μ¨μ μ΄λΌλ κ²μ μκ² λμλ€.
μ΄μ°¨νΌ board
λ κ³ μ μ΄κ³ μ°λ¦¬λ λ‘λ΄μ μμΉλ§ λ³κ²½μν€λ©΄μ νμμ μννλ©΄ λλ―λ‘ board
λμ λ‘λ΄μ μ£νκ°μ ν΅ν΄ νμμ μννλλ‘ νμλ€.
λ¬Έμ νμ΄ ν λ€λ₯Έ μ¬λλ€μ μ½λλ λ΄€λλ°
visited
λ°°μ΄μ λ°λ‘ μ μ§νκ³ Queueμ κ±Έλ¦° μκ°μ μ’νκ°κ³Ό ν¨κ» λ£μ΄μ€ κ²μ΄ μΈμμ μ΄μλ€.
μ΄λ κ² νλ©΄ hashable κ°μ²΄λ‘ λ°κΏμ λμ λ리μ λ£μΌλ €κ³ κ΅³μ΄ listλ₯Ό tupleλ‘ λ³κ²½νμ§ μμλ λ κ² κ°λ€.
λ‘λ΄ νμ μν€κΈ°
μ΄λ² λ¬Έμ μμ κ°μ₯ κΉλ€λ‘μ λ νμ μ°μ°μ ꡬννκΈ° μν΄μλ μ,ν,μ’,μ° μ΄λκ³Ό 4κ°μ§μ νμ μ°μ°μ λͺ¨λ 체ν¬ν΄μ€μΌνλ€.
μνμ’μ° μ’νκ°λ€μ κ²μ¬νλ건 λ€λ₯Έ λ¬Έμ λ€κ³Ό λ€λ₯Έ κ²μ΄ μμ§λ§ νμ μ°μ°μ΄ κ°λ₯νμ§λ μ΄λ»κ² μ μ μμκΉ?
μκ°ν΄λ³΄λ©΄ νμ μ°μ°μ μν΄μλ νμ νκ³ μΆμ λ°©ν₯μ΄ λ¬΄μ‘°κ±΄ λͺ¨λ λΉμ΄μλ μνμ¬μΌ νλ€λ κ²μ μ μ μλ€.
μλμ κ°μ΄ νκ΅°λ°λΌλ λ²½μ΄ μλ€λ©΄ κ·Έ μͺ½μΌλ‘λ νμ μ΄ λΆκ°λ₯νλ€.
λ°λΌμ λλ μ΄μ μ μ,ν,μ’,μ° μ€μμ μ΄λμ΄ κ°λ₯νλ μ°μ°λ€μ μ μ₯ν΄λμ λ€μ, κ·Έμ λ§λ νμ μ£νκ°λ€μ μμ±ν΄μ€¬λ€.
μ΄λ μ£Όμν μ μ λμ ꡬν λ°©μμ κΈ°μ€μΌλ‘ νμ λ μ’νκ°μ λ‘λ΄μ λ μ’ν μ€ νμ μΌμͺ½ or μλ¨μ μ’νκ° λ¨Όμ λμ€λλ‘ μμ±ν΄μ€μΌ νλ€λ κ²μ΄λ€.
μ΄κ±° λλ¬Έμ μ²μμ κ³μ μ€ν¨νμλ€..
μ½λ
from collections import deque
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
def move(board, here):
N = len(board)
movable = []
movable_dir = [] # μ΄λ κ°λ₯ν λ°©ν₯
# μνμ’μ°
for i in range(4):
next = [0, 0, 0, 0]
next[0] = here[0] + dx[i]
next[1] = here[1] + dy[i]
next[2] = here[2] + dx[i]
next[3] = here[3] + dy[i]
if next[0] < 0 or next[0] >= N or next[1] < 0 or next[1] >= N:
continue
if next[2] < 0 or next[2] >= N or next[3] < 0 or next[3] >= N:
continue
if board[next[0]][next[1]] == 1 or board[next[2]][next[3]] == 1:
continue
movable.append(tuple(next))
movable_dir.append(i)
# νμ
axis1 = here[:2]
axis2 = here[2:]
# κ°λ‘λ‘ λμ¬μμ κ²½μ°
if axis1[0] == axis2[0]:
# μλκ° λ€ λΉμ΄μ μ΄λ κ°λ₯ν κ²½μ°
if 2 in movable_dir:
there1 = axis1 + (axis2[0] + 1, axis2[1] - 1)
there2 = axis2 + (axis1[0] + 1, axis1[1] + 1)
movable.append(there1)
movable.append(there2)
# μμκ° λ€ λΉμ΄μ μ΄λ κ°λ₯ν κ²½μ°
if 3 in movable_dir:
there1 = (axis2[0] - 1, axis2[1] - 1) + axis1
there2 = (axis1[0] - 1, axis1[1] + 1) + axis2
movable.append(there1)
movable.append(there2)
# μΈλ‘λ‘ λμ¬μμ κ²½μ°
else:
# μ€λ₯Έμͺ½μ΄ λ€ λΉμ΄μ μ΄λ κ°λ₯ν κ²½μ°
if 0 in movable_dir:
there1 = axis1 + (axis2[0] - 1, axis2[1] + 1)
there2 = axis2 + (axis1[0] + 1, axis1[1] + 1)
movable.append(there1)
movable.append(there2)
# μΌμͺ½μ΄ λ€ λΉμ΄μ μ΄λ κ°λ₯ν κ²½μ°
if 1 in movable_dir:
there1 = (axis2[0] - 1, axis2[1] - 1) + axis1
there2 = (axis1[0] + 1, axis1[1] - 1) + axis2
movable.append(there1)
movable.append(there2)
return movable
def solution(board):
# μ²μ λ‘λ΄ μμΉ
start = (0, 0, 0, 1)
N = len(board)
cost = {} # κ±Έλ¦° μκ°
q = deque()
cost[start] = 0
q.append(start)
while True:
here = q[0]
q.popleft()
if here[0] == N-1 and here[1] == N-1 or here[2] == N-1 and here[3] == N-1:
answer = cost[here]
break
move_list = move(board, here)
for there in move_list:
if there not in cost:
cost[there] = cost[here] + 1
q.append(there)
return answer
board = [[0, 0, 0, 0, 0],[1, 1, 1, 0, 0],[1, 1, 1, 1, 0],[1, 1, 1, 1, 0],[1, 1, 1, 0, 0]]
print(solution(board)) # κ²°κ³Όλ 7
'π algorithm > programmers' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
νλ‘κ·Έλλ¨Έμ€ Level 2 - ν° μ λ§λ€κΈ° (0) | 2021.03.01 |
---|---|
νλ‘κ·Έλλ¨Έμ€ Level 3 - μ¬ μ°κ²°νκΈ° (0) | 2021.03.01 |
νλ‘κ·Έλλ¨Έμ€ Level 4 - κ²μ 맡 μ΅λ¨κ±°λ¦¬ (0) | 2021.03.01 |
νλ‘κ·Έλλ¨Έμ€ Level 3 - μ κ΅ μ¬μ¬ (0) | 2021.03.01 |
νλ‘κ·Έλλ¨Έμ€ Level 3 - μΆμ νΈλν½ (0) | 2021.03.01 |
π¬ λκΈ