def solution(n):
result = []
checked = []
# μ΅μ’
λλ²
final = (n) * (n + 1) // 2
# κ·Έλνμ λ°©λ¬Έ μ¬λΆ checked μ΄κΈ°ν
for i in range(1, n + 1):
arr = [j + 1 for j in range(i)]
chk = [False for j in range(i)]
result.append(arr)
checked.append(chk)
# μμ§μΌ μ μλ λ°©ν₯ μΌμͺ½ μλ λ°©ν₯ λΆν° μ§ν
dr = [1, 0, -1]
dc = [0, 1, -1]
direct = 0
# 0,0 1λ² λΆν° λλ²λ§ μμ
num = 1
r, c = 0, 0
while True:
# λλ²λ§μ΄ μ΅μ’
λλ²μ λμΌν΄μ§λ©΄ 루ν μ’
λ£
if num == final:
result[r][c] = final
break
# λλ²λ§ νλ©΄μ μ²΄ν¬ μ§ν
result[r][c] = num
checked[r][c] = True
num += 1
# λ λμκ° μ μκ±°λ μ΄λ―Έ λ°©λ¬Ένλ μΉΈμ λ§λλ©΄ λ°©ν₯ μ ν
while True:
nr, nc = dr[direct] + r, dc[direct] + c
if 0 <= nr < n and nc < n and not checked[nr][nc]:
break
else:
direct = (direct + 1) % 3
r, c = nr, nc
answer = []
for res in result:
answer += res
return answer
λ°©ν₯ μ€μ μΌλ‘ λλ²λ§ νλ λ°©μ μ¬μ©