728x90
import itertools
# 달팽이가 움직인 좌표
def get_next(x, y, d):
DELTAS = {'up': (-1, -1), 'down': (1, 0), 'right': (0, 1)}
dx, dy = DELTAS[d][0], DELTAS[d][1]
nx, ny = x + dx, y + dy
return nx, ny
# 달팽이가 범위 안에 있는지 밖에 있는지 확인
def check_turn(nx, ny, n, snail):
return nx < 0 or nx >= n or ny > nx or snail[nx][ny] != 0
def solution(n):
NEXT = {"up" : "down", "down" : "right", "right" : "up"}
snail = [[0] * i for i in range(1, n + 1)]
N = sum(range(1, n + 1))
x, y, d = 0, 0, "down"
for num in range(1, N + 1):
snail[x][y] = num
if check_turn(*get_next(x, y, d), n, snail): # if true, turn direction
d = NEXT[d]
x, y = get_next(x, y, d)
return list(itertools.chain(*snail))
airvw.github.io/algorithm/2020-10-30-programmers-68645/
삼각 달팽이(python)
참고자료
airvw.github.io
삼각 달팽이(python)
참고자료
airvw.github.io
[프로그래머스 lv2, lv3 을 풀자!] 삼각 달팽이
programmers.co.kr/learn/courses/30/lessons/68645 코딩테스트 연습 - 삼각 달팽이 5 [1,2,12,3,13,11,4,14,15,10,5,6,7,8,9] 6 [1,2,15,3,16,14,4,17,21,13,5,18,19,20,12,6,7,8,9,10,11] programmers.co.kr 문..
weejw.tistory.com
728x90
'Algorithm' 카테고리의 다른 글
[프로그래머스] 조이스틱 (0) | 2021.03.05 |
---|---|
[프로그래머스] 큰 수 만들기 (0) | 2021.03.04 |
[프로그래머스] 문자열 압축 파이썬 (1) | 2021.03.03 |
[프로그래머스] 주식가격 파이썬 (0) | 2021.03.03 |
[프로그래머스] 스킬트리 파이썬 (0) | 2021.03.03 |
댓글