프로그래머스

프로그래머스 - 방문 길이

yanJuicy 2024. 1. 6. 03:14
반응형

문제

https://school.programmers.co.kr/learn/courses/30/lessons/49994

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

풀이

구현 문제다

[현재 좌표, 이동할 좌표]로 배열을 만들어 걸어본 길을 저장한다.

걸어본 길은 양방향으로 확인한다.

[현재 좌표, 이동할 좌표], [이동할 좌표, 현재 좌표]를 확인 한 후 걸어본 길을 저장한다.

좌표가 중복이 안되게 체크하고, 이동할 좌표도 좌표 평면의 길이를 넘지 않도록 주의한다.

 

 

 

코드

python

def solution(dirs):
    
    traces = []
    current = [0, 0]
    
    for dir in dirs:
        if dir == "U": 
            next = [current[0] + 1, current[1]]
        elif dir == "D": 
            next = [current[0] - 1, current[1]]
        elif dir == "R": 
            next = [current[0], current[1] + 1]
        else: 
            next = [current[0], current[1] - 1]
            
        if next[0] > 5 or next[0] < -5 or next[1] > 5 or next[1] < -5:
            continue
            
        if current + next not in traces and next + current not in traces: # 양방향
            traces.append(current + next)
        current = next
            
    answer = len(traces)
    return answer
반응형