Link : programmers.co.kr/learn/courses/30/lessons/49994
Python
더보기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | def solution(dirs): result = 0 history_path = dict() pos = (0, 0) # X Y for i in dirs: if i == 'L': if pos[0] - 1 < -5: continue if (pos[0], pos[1], 'L') not in history_path: history_path[(pos[0], pos[1], 'L')] = None history_path[(pos[0] - 1, pos[1], 'R')] = None result += 1 pos = (pos[0] - 1, pos[1]) elif i == 'R': if pos[0] + 1 > 5: continue if (pos[0], pos[1], 'R') not in history_path: history_path[(pos[0], pos[1], 'R')] = None history_path[(pos[0] + 1, pos[1], 'L')] = None result += 1 pos = (pos[0] + 1, pos[1]) elif i == 'U': if pos[1] - 1 < -5: continue if (pos[0], pos[1], 'U') not in history_path: history_path[(pos[0], pos[1], 'U')] = None history_path[(pos[0], pos[1] - 1, 'D')] = None result += 1 pos = (pos[0], pos[1] - 1) elif i == 'D': if pos[1] + 1 > 5: continue if (pos[0], pos[1], 'D') not in history_path: history_path[(pos[0], pos[1], 'D')] = None history_path[(pos[0], pos[1] + 1, 'U')] = None result += 1 pos = (pos[0], pos[1] + 1) return result | cs |
FeedBack
cjw.git@gmail.com
'알고리즘 > 소스코드' 카테고리의 다른 글
PROGRAMMERS 17684: [3차] 압축 (0) | 2021.04.05 |
---|---|
PROGRAMMERS 1844: 게임 맵 최단거리 (0) | 2021.04.02 |
PROGRAMMERS 68936: 쿼드압축 후 개수 세기 (0) | 2021.04.02 |
PROGRAMMERS 72410: 신규 아이디 추천 (0) | 2021.03.24 |
PROGRAMMERS 42628: 이중우선순위큐 (0) | 2021.03.24 |
댓글