[Level2] 방문 길이

Quesuemon·2021년 3월 29일
0

코딩테스트 준비

목록 보기
31/111

🛠 문제

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


👩🏻‍💻 해결 방법

dictionary와 set을 적절히 활용하는 문제였다
이미 지나간 길은 무시하기 위해서 add를 두번 해주어야 했다

소스 코드

def solution(dirs):
    d = {'U':(0, -1), 'D':(0, 1), 'L':(-1,0), 'R':(1,0)}
    move = set()
    cur_x, cur_y = (0, 0)
    
    for i in dirs:
        next_x, next_y = cur_x + d[i][0], cur_y + d[i][1]
        if -5 <= next_x <= 5 and -5 <= next_y <= 5:
            move.add((cur_x, cur_y, next_x, next_y))
            move.add((next_x, next_y, cur_x, cur_y))
            cur_x, cur_y = next_x, next_y
            
    return len(move) // 2

0개의 댓글