Coding Test/백준(Python)

[백준/Python] 1107번(구현)_리모컨

syunze 2024. 3. 12. 13:57

📌문제 유형

브루트포스(골5)

 

📌문제

 

1107번: 리모컨

첫째 줄에 수빈이가 이동하려고 하는 채널 N (0 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 고장난 버튼의 개수 M (0 ≤ M ≤ 10)이 주어진다. 고장난 버튼이 있는 경우에는 셋째 줄에는 고장난 버튼이

www.acmicpc.net

 

 

📌나의 문제풀이

- 틀림. 

# https://www.acmicpc.net/problem/1107
# 리모컨

from itertools import product

n = input()
m = int(input())
breakdown = list(map(int,input().split()))

normal = [i for i in range(10)]
for num in breakdown:
    if num in normal:
        normal.remove(num)

result = abs(100 - int(n))
max_num, min_num = 0,0

if len(normal) > 0:
    first = str(normal[0])
    if normal[0] == 0 and len(normal) > 1:
        first = str(normal[1])
    max_num = int(first + '0' * len(n))

    last = str(normal[-1])
    min_num = int(last * (len(n)-1))

    if abs(min_num - int(n)) + len(str(min_num)) < result:
        result = abs(min_num - int(n))+ len(str(min_num))
    if abs(max_num - int(n)) + len(str(max_num)) < result:
        result = abs(max_num - int(n))+ len(str(max_num))

nums = list(product(normal, repeat = len(n)))
for num in nums:
    now = int(''.join(map(str,num)))
    tmp = len(list(str(now)))
    # print('지금 숫자', now)
    # print(tmp)
    tmp += (abs(int(n) - now))
    # print('최종',tmp)
    if tmp < result:
        result = tmp

print(result)

 

📌다른사람의 문제풀이

 

[백준] 1107번 리모컨 - 파이썬(Python)

문제 (링크) https://www.acmicpc.net/problem/1107 1107번: 리모컨 첫째 줄에 수빈이가 이동하려고 하는 채널 N (0 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 고장난 버튼의 개수 M (0 ≤ M ≤ 10)이 주어진다. 고장

seongonion.tistory.com

import sys
input = sys.stdin.readline
target = int(input())
n = int(input())
broken = list(map(int, input().split()))

# 현재 채널에서 + 혹은 -만 사용하여 이동하는 경우
min_count = abs(100 - target)

for nums in range(1000001):
    nums = str(nums)
    
    for j in range(len(nums)):
        # 각 숫자가 고장났는지 확인 후, 고장 났으면 break
        if int(nums[j]) in broken:
            break

        # 고장난 숫자 없이 마지막 자리까지 왔다면 min_count 비교 후 업데이트
        elif j == len(nums) - 1:
            min_count = min(min_count, abs(int(nums) - target) + len(nums))

print(min_count)

 

📌리뷰

- .... 정말 일일이 탐색하는 문제였다니 충격

- 이 쉬운걸 몇시간동안 붙잡고 있는건지,,,

728x90