내 풀이
def solution(dirs):
answer = 0
cur_location = (0,0) # 현재 위치
path = set() # 겹치는 것은 제외
# 현재 위치 & 방향 -> 다음 위치
def move(current, direction):
x, y = current
if direction == "U":
y += 1
elif direction == "D":
y -= 1
elif direction == "R":
x += 1
elif direction == "L":
x -= 1
if not -5 <= x <=5 or not -5 <= y <= 5: # 범위를 벗어나면 현재에 머물도록
return current
return (x,y)
for d in dirs:
next_location = move(cur_location, d)
if str(cur_location) == str(next_location): # 움직이지 못한 경우는 다음으로
continue
# 방향을 무시하기 위해 sorted 사용
path.add(str(sorted((str(cur_location), str(next_location)))))
cur_location = next_location # 위치 업데이트
return len(path) # 경로의 개수
메모
set에는 set을 더할 수 없다.
'PROGRAMMERS' 카테고리의 다른 글
[프로그래머스/파이썬] [3차] 압축 (0) | 2025.01.20 |
---|---|
[프로그래머스/파이썬] k진수에서 소수 개수 구하기 (0) | 2025.01.17 |
[프로그래머스/파이썬] 롤케이크 자르기 (0) | 2025.01.15 |
[프로그래머스/파이썬] 튜플 (0) | 2025.01.14 |
[프로그래머스/파이썬] [1차] 캐시 (0) | 2025.01.13 |