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

 

문제

def solution(dirs):
    answer = 0
    x = range(-5, 6)
    y = range(-5, 6)
    cur = [0,0]
    visited = []
    for i in dirs:
        prev = []
        prev.append(cur[0])
        prev.append(cur[1])
        if i == 'L' and cur[0] - 1 >= -5:
            cur[0] -= 1
        elif i == 'R' and cur[0] + 1 <= 5:
            cur[0] += 1
        elif i == 'U' and cur[1] + 1 <= 5:
            cur[1] += 1
        elif i == 'D' and cur[1] - 1 >= -5:
            cur[1] -= 1
        else:
            continue
        
        c = []
        c.append(cur[0])
        c.append(cur[1])
        if [prev, c] not in visited and [c, prev] not in visited:
            visited.append([prev, c])
            visited.append([c, prev])
            answer+=1
    return answer