본문 바로가기

Coding Test/Programmers

[프로그래머스/파이썬] 방문 길이

728x90

 

 

나의 풀이

def solution(dirs):
    direction = {'L': (-1, 0), 'U': (0, -1), 'R': (1, 0), 'D': (0, 1)}
    visited = set()
    x, y = 0, 0

    for d in dirs:
        nx, ny = x + direction[d][0], y + direction[d][1]
        if -5 <= nx <= 5 and -5 <= ny <= 5:
            visited.add((min(x, nx), min(y, ny), max(x, nx), max(y, ny)))
            x, y = nx, ny

    return len(visited)

 

 

 

 

 

반응형