-
[백준] 1107번 리모컨 (by Python)Programming/Algorithm 2021. 2. 28. 19:51
문제
https://www.acmicpc.net/problem/1107
풀이
nums = set(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']) n = int(input()) m = int(input()) if m: broken = set(input().split()) nums -= broken ans = abs(n - 100) if nums: i = 0 while True: left = 0 if n - i <= 0 else n - i right = n + i left_flag = all(l in nums for l in str(left)) right_flag = all(l in nums for l in str(right)) if left_flag or right_flag: if left_flag: ans = min(ans, i + len(str(left))) if right_flag: ans = min(ans, i + len(str(right))) break i += 1 print(ans)
100에서 +-를 통해서만 n에 도달하거나 n에 가장 가까운 번호를 입력 후 +-로 도달하는 두 가지 방법이 있다고 판단해 n에서 가장 가까운 만들 수 있는 수를 탐색하고, |n - 100| 값을 구해 둘 중 더 cost가 낮은 값을 선택하는 방식을 취했다.
두 가지 정도 주의할 점이 있었는데, 첫 번째는 left 값과 right 값이 모두 만들 수 있는 수인데 left가 자릿수가 더 낮을 경우 left를 취해야 하므로 이를 고려해야 했단 점이다. 그리고 두 번째는 m이 0일 경우 3번째 라인이 주어지지 않는다는 점이다. 필자는 이 점에서 실수를 해 처음 EOFError를 마주해야했다...
아무쪼록 즐거운 코딩하시길!
'Programming > Algorithm' 카테고리의 다른 글
[백준] 1525번 퍼즐 (by Python) (0) 2021.03.05 [백준] 14889번 스타트와 링크 (by Python) (0) 2021.03.02 [백준] 9663번 N-Queen (by Python) (0) 2021.02.28 [백준] 2225번 합분해 (by Python) (0) 2021.02.28 [백준] 1699번 제곱수의 합 (by Python) (0) 2021.02.28