[Programmers] - 빛의 경로 사이클 (Level 2)


월간 코드 챌린지 시즌3
LEVEL : 2
문제링크

grid = ["SLR"]
def solution(grid):
    import numpy as np
    # Right, Down, Left, Up
    pos = [[0,1], [1,0], [0,-1], [-1,0]]

    answer = []
    cycle = []

    check = (np.ones((len(grid), len(grid[0]), 4)) == 0)

    for i in range(len(grid)):
        for j in range(len(grid[0])):
            for k in range(4):
                r,c,d = i,j,k
                cnt = 0
                while check[r,c,d]==False:
                    cnt+=1
                    check[r,c,d] = True
                    # direction update
                    # d = 0, 1, 2, 3 = Right, Down, Left, Up
                    if grid[r][c] == 'R':
                        d = (d+1)%4

                    elif grid[r][c] == 'L':
                        d = (d-1)%4

                    # position update
                    r = (r + pos[d][0])%len(grid)
                    c = (c + pos[d][1])%len(grid[0])

                if cnt > 0:
                    answer.append(cnt)

    return sorted(answer)
solution(grid)
[1, 1, 4, 6]

Comments