본문 바로가기
코딩 테스트/문제 풀기

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

by hazel_ 2021. 3. 15.
def solution(dirs):
    x = 0
    y = 0

    dx=[0,0,-1,1]
    dy=[-1,1,0,0]
    # 상 0 하 1 좌 2 우 3
    d = {"U":0, "D":1, "L":2, "R":3}

    visited=[]
    count=0

    for dir in dirs:
        go = d[dir]
        nx = x + dx[go]
        ny = y + dy[go]

        if nx >= -5 and ny >= -5 and nx <= 5 and ny <=5:
            if (x, y, nx, ny) not in visited:
                visited.append((x, y, nx, ny))
                visited.append((nx, ny, x, y))
                count+=1
        else:
            continue

        x, y = nx, ny

    return count


dirs="LULLLLLLU"
print(solution(dirs))

댓글