반응형
Notice
Recent Posts
Recent Comments
Link
동캄의 코딩도장
백준 1107 [리모컨] 파이썬 본문
반응형
https://www.acmicpc.net/problem/1107
1107번: 리모컨
첫째 줄에 수빈이가 이동하려고 하는 채널 N (0 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 고장난 버튼의 개수 M (0 ≤ M ≤ 10)이 주어진다. 고장난 버튼이 있는 경우에는 셋째 줄에는 고장난 버튼
www.acmicpc.net
# 백준 1107 리모컨
from itertools import product
num = input()
want = list(map(int, num))
l = len(want)
num = int(num)
n = int(input())
if n == 0:
use = [i for i in range(10)]
else:
delbtn = list(map(int, input().split()))
use = list((set([i for i in range(10)]))-set(delbtn))
use.sort()
if use:
if not (len(use) == 1 and use[0] == 0):
if use[0] == 0:
big = int(str(use[1])+str(use[0])*l)
else:
big = int(str(use[0])*(l+1))
if l != 1:
small = int(str(use[-1])*(l-1))
else:
small = -1000000
answer = min(abs(num-100), l-1+(num-small), l+1+(big-num))
else:
answer = min(abs(num-100), 1+num)
else:
answer = abs(num-100)
use = list(map(str, use))
data = product(use, repeat=l)
for p in data:
val = int(''.join(p))
answer = min(answer, abs(val-num)+l)
print(answer)
n 번째 숫자로 가는 가장 짧은 횟수는
min ( 위아래 버튼만 움직이기, 한자리가 더 긴 수에서 아래 버튼만 움직이기, 한자리가 더 작은 수에서 위 버튼만 움직이기, 사용가능한 모든 숫자로 이동한 뒤, 위 아래로 움직이기) 이므로,
위의 경우를 모두 따져주면 된다,
되게 간단한 문제라고 생각했는데... 어렵다. 맞추긴 했지만, 코드가 너무 지저분하다...
++ from itertools import product 에 대해 알아두자
++ 코드를 잘짜자
반응형
'코테 > BOJ' 카테고리의 다른 글
백준 10026 [적록색약] 파이썬 (0) | 2022.04.14 |
---|---|
백준 5430 [AC] 파이썬 (0) | 2022.04.12 |
백준 1987 [알파벳] 파이썬 (0) | 2022.03.24 |
백준 7576 [토마토] 파이썬 (0) | 2022.03.21 |
백준 11866 [요세푸스 문제 0] 파이썬 (0) | 2022.03.07 |