Coding Test
[프로그래머스] 방문 길이
상상쓰
2021. 5. 30. 22:28
https://programmers.co.kr/learn/courses/30/lessons/49994
코딩테스트 연습 - 방문 길이
programmers.co.kr
거쳤던 길을 제거하기 위해서 set 에 담는다. 가는 길과 돌아오는 길을 set 에 넣어줘야 하기 때문에 return 값은 set 의 길이를 2 로 나눠주면 된다.
def solution(dirs):
answer = 0
route = set()
x, y, x_, y_= 0, 0, 0, 0
for i in dirs:
if i == 'U':
x_, y_ = x, y + 1
if i == 'D':
x_, y_ = x, y - 1
if i == 'R':
x_, y_ = x + 1, y
if i == 'L':
x_, y_ = x - 1, y
if abs(x_) <= 5 and abs(y_) <= 5:
route.add((x, y, x_, y_))
route.add((x_, y_, x, y))
x, y = x_, y_
answer = len(route) // 2
return answer
print(solution('LULLLLLLU')) # 7