Post

[BOJ] 1107. ๋ฆฌ๋ชจ์ปจ(python)

๐Ÿ“Œ๋ฌธ์ œ

Alt text

๐Ÿ’ช์•„์ด๋””์–ด

  1. ๊ณ ์žฅ๋‚œ ๋ฒ„ํŠผ ์ฒดํฌ
    ๊ณ ์žฅ๋‚œ ๋ฒ„ํŠผ์ด ์—†๋Š” ๊ฒฝ์šฐ ๋ฒ„ํŠผ๊ณ ์žฅ ์ฒ˜๋ฆฌ ๊ณผ์ •์„ ๊ฑด๋„ˆ๋›ด๋‹ค.

  2. ๋ฒ„ํŠผ ๋ˆ„๋ฅด๊ธฐ
    ํ˜„์žฌ ์ฑ„๋„๊ณผ ๋ชฉํ‘œ ์ฑ„๋„์ด ๊ฐ™์œผ๋ฉด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
    ํ˜„์žฌ ์ฑ„๋„๊ณผ ๋ชฉํ‘œ ์ฑ„๋„์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ ๋ฆฌ๋ชจ์ปจ์„ ์‚ฌ์šฉํ•œ๋‹ค.

    • ์ˆซ์ž ๋ฒ„ํŠผ ๋ˆ„๋ฅด๊ธฐ
      ๋ชฉํ‘œ ์ฑ„๋„ ์ˆซ์ž์— ๊ณ ์žฅ๋‚œ ๋ฒ„ํŠผ์˜ ์ˆซ์ž๊ฐ€ ํฌํ•จ๋˜๋ฉด ๊ฑด๋„ˆ๋›ด๋‹ค.
    • ์•„๋ž˜, ์œ„ ๋ฒ„ํŠผ ๋ˆ„๋ฅด๊ธฐ
      ์ˆซ์ž ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ํ›„๋‚˜ ํ˜„์žฌ ์ฑ„๋„์—์„œ ์œ„ ์•„๋ž˜ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์„œ ๋ชฉํ‘œ ์ฑ„๋„๋กœ ๋งž์ถ˜๋‹ค.

๐Ÿฅ‚์ฝ”๋“œ

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
import sys; input = sys.stdin.readline

target = int(input())

m = int(input())
broken=set()
if m!=0:
    broken = set(input().split())

if target == 100: 
    print(0)
    exit(0)

answer =float('inf')

def canPushButton(channel):
    for c in str(channel):
        if c in broken: return False
    return True

for channel in range(1000001):
    cnt = 0
    if channel==100:
        cnt = abs(target-100)

    elif canPushButton(channel):
        cnt = len(str(channel))
        if channel!=target:
            cnt += abs(channel - target)
    else: continue
    answer = min(answer,cnt)

print(answer)
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.