알고리즘/소스코드
PROGRAMMERS 49994: 방문 길이
cjw.git
2021. 4. 2. 22:32
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