문제 링크
풀이 과정
전체 코드
def isin_boundary(cy, cx):
'''
y: 세로 방향의 좌표
x: 가로 방향의 좌표
'''
return -5 <= cy <= 5 and -5 <= cx <= 5
def solution(dirs):
answer = 0
directions = {'U': (-1, 0), 'L': (0, -1), 'R': (0, 1), 'D': (1, 0)}
traces = set()
cx, cy = 0, 0
for direction in dirs:
dy, dx = directions[direction]
ny, nx = cy + dy, cx + dx
if isin_boundary(ny, nx):
if (cy, cx, ny, nx) not in traces:
traces.add((cy, cx, ny, nx))
traces.add((ny, nx, cy, cx))
cy, cx = ny, nx
return len(traces) / 2
Python
복사