본문 바로가기
Algorithm

[프로그래머스] 삼각 달팽이

by YGSEO 2021. 3. 4.
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

weejw.tistory.com/442

 

[프로그래머스 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

댓글