Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

22-gjsk132 #106

Merged
merged 2 commits into from
Feb 17, 2024
Merged

22-gjsk132 #106

merged 2 commits into from
Feb 17, 2024

Conversation

gjsk132
Copy link
Member

@gjsk132 gjsk132 commented Feb 14, 2024

πŸ”— 문제 링크

λ°±μ€€ 1107 : 리λͺ¨μ»¨

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„

✨ μˆ˜λ„ μ½”λ“œ

🎯 문제 이해

μ•Œκ³ λ¦¬μ¦˜ λΆ„λ₯˜ : 브루트포슀(Brute Force)

좜처 : λ‚˜λ¬΄μœ„ν‚€

+와 - λ²„νŠΌμœΌλ‘œ 채널을 각각 +1, -1μ”© 움직일 수 μžˆλ‹€.
0 ~ 9 λ²„νŠΌμ„ λˆŒλŸ¬μ„œ μ›ν•˜λŠ” μ±„λ„λ‘œ 이동할 수 μžˆλ‹€.
ν•˜μ§€λ§Œ, 숫자 λ²„νŠΌ μ€‘μ—λŠ” μ‚¬μš©ν•  수 μ—†λŠ” μˆ«μžκ°€ μžˆλ‹€.

채널은 0λΆ€ν„° λ¬΄ν•œλŒ€κΉŒμ§€ 있으며, ( 0μ—μ„œ - λ²„νŠΌμ„ λˆ„λ₯΄λ©΄ κ·ΈλŒ€λ‘œ 0 이닀. )
100번 μ±„λ„μ—μ„œ μ‹œμž‘ν•΄μ„œ μ›ν•˜λŠ” μ±„λ„λ‘œ 갈 λ•Œ, λ²„νŠΌμ„ μ΅œμ†Œ λͺ‡ 번 λˆŒλŸ¬μ•Ό ν•˜λŠ”μ§€λ₯Ό 좜λ ₯ν•œλ‹€.


μ›ν•˜λŠ” μ±„λ„μ˜ μžλ¦Ώμˆ˜λ§ˆλ‹€ 경우의 수λ₯Ό μƒκ°ν•΄μ„œ κ²°κ³Όλ₯Ό λ‚Ό 수 μžˆμ§€λ§Œ μƒκ°ν•˜κΈ°μ—” λ³΅μž‘ν•  것 κ°™λ‹€. κ·Έλž˜μ„œ λ²„νŠΌμ„ λˆŒλŸ¬μ„œ 접근이 κ°€λŠ₯ν•œκ°€ μ•„λ‹Œκ°€λ₯Ό νŒλ‹¨ν•˜λŠ” `cannel`μ΄λΌλŠ” 리슀트λ₯Ό λ§Œλ“€μ–΄μ„œ μ‚¬μš©ν•  것이닀.

μ›ν•˜λŠ” μ±„λ„λ‘œ κ°€λ €λŠ” 경우의 μˆ˜λŠ” 크게 2κ°€μ§€λ‘œ λ‚˜λˆ„μ–΄μ§ˆ 수 μžˆλ‹€.

1. 채널을 움직이지 μ•Šκ³ , μœ„ μ•„λž˜λ§Œ μ‚¬μš©ν•˜λŠ” 경우
2. 채널을 움직인 ν›„, μœ„ μ•„λž˜λ‘œ μ›€μ§μ΄λŠ” 경우

1번 κ²½μš°λŠ” κ°„λ‹¨ν•˜κ²Œ μ›ν•˜λŠ” 채널 κ°’κ³Ό ν˜„μž¬ μœ„μΉ˜ (100)의 차이둜 μ•Œ 수 μžˆλ‹€.

2번 κ²½μš°λŠ” μ•žμ„œ λ§Œλ“€μ–΄λ‘” cannelμ΄λΌλŠ” 리슀트λ₯Ό ν™œμš©ν•œλ‹€.

λͺ©ν‘œ 채널을 κΈ°μ€€μœΌλ‘œ μœ„ μ•„λž˜ λ²”μœ„λ₯Ό 1μ”© λŠ˜λ €κ°€λ©΄μ„œ λ²„νŠΌμ„ λˆŒλŸ¬μ„œ μ ‘κ·Όν•  수 μžˆλŠ” κ°€μž₯ κ°€κΉŒμš΄ 채널을 μ°ΎλŠ”λ‹€.

μ ‘κ·Ό κ°€λŠ₯ν•œ λ²„νŠΌμ„ λ§Œλ‚¬μ„ λ•Œ, ν•΄λ‹Ή 번호λ₯Ό λˆ„λ₯΄κΈ° μœ„ν•΄ ν•„μš”ν•œ λ²„νŠΌ 횟수λ₯Ό 더해주면 λœλ‹€.

λ§ˆμ§€λ§‰μœΌλ‘œ 1번과 2번의 κ²°κ³Ό μ€‘μ—μ„œ μ΅œμ†Ÿκ°’μ„ 좜λ ₯ν•΄μ£Όλ©΄ λœλ‹€.

πŸ”μ½”λ“œ μ„€λͺ…

target : λͺ©ν‘œ 채널
cannel : μ±„λ„μ˜ μ ‘κ·Ό μ—¬λΆ€λ₯Ό μ•Œ 수 μžˆλŠ” boolean 리슀트
limit : cannel의 μ œν•œ
upDown : μœ„ μ•„λž˜λ§Œ μ΄λ™ν•˜λŠ” 경우 μ΅œμ†Œ λ²„νŠΌ 횟수 (1번)
cnt : λͺ©ν‘œ 채널과 λˆ„λ₯Ό 수 μžˆλŠ” κ°€μž₯ κ°€κΉŒμš΄ λ²„νŠΌκ³Όμ˜ 거리 (2번)
brokenCnt : κ³ μž₯λ‚œ λ²„νŠΌμ˜ 수
broken : κ³ μž₯λ‚œ λ²„νŠΌλ“€

μœ„ λ³€μˆ˜μ˜ λͺ©μ μ— λ§žμΆ°μ„œ μž…λ ₯을 λ°›κ³ , 리슀트λ₯Ό λ§Œλ“€μ–΄μ€€λ‹€.

cannel = []
target = int(input())
limit = target*2 + 100

# μœ„ μ•„λž˜ μ΄λ™ν•˜λŠ”κ±΄ μ›ν•˜λŠ” 채널과 μ‹œμž‘ 채널(100)의 차이이닀.
upDown = target - 100 if target > 100 else 100 - target

cnt = 0

μš°μ„  κ³ μž₯λ‚œ λ²„νŠΌμ΄ μžˆλ‹€λ©΄ κ·Έ λ²„νŠΌμ— λŒ€ν•œ 정보λ₯Ό 리슀트둜 μž…λ ₯λ°›κ³  2번 과정을 μ§„ν–‰ν•œλ‹€.
λ°˜λ©΄μ—, κ³ μž₯λ‚œ λ²„νŠΌμ΄ μ—†λ‹€λ©΄ 2번 과정을 μŠ€ν‚΅ν•˜κ³ , λͺ©ν‘œ 채널을 λˆ„λ₯΄κΈ° μœ„ν•œ λ²„νŠΌ 횟수만 cnt에 더해쀀닀.
( 2번 : λ²„νŠΌμ„ λˆ„λ£° 수 μžˆλŠ” 채널 μ€‘μ—μ„œ λͺ©ν‘œ μ±„λ„μ—μ„œ κ°€μž₯ κ°€κΉŒμš΄ 채널을 μ°ΎλŠ” κ³Όμ • )

if (brokenCnt := int(input())):
    broken = list(map(str,input().split()))
    # ( 2번 κ³Όμ • : 핡심 μ½”λ“œ )
else:
    cnt += len(str(target))

핡심 μ½”λ“œ

for i in range(limit):
    cannel.append(False if any(b in str(i) for b in broken) else True)

while cnt < upDown:
    
    if (cannel[target-cnt] if target>=cnt else False):
        cnt += len(str(target-cnt))
        break
    
    if (cannel[target+cnt] if target+cnt < limit else False):
        cnt += len(str(target+cnt))
        break
    
    cnt += 1

for 반볡문
채널 번호의 λͺ¨λ“  번호의 λ²„νŠΌμ΄ λΆ€μ„œμ‘ŒλŠ”μ§€ ν™•μΈν•˜λŠ” 과정이닀.
λ§Œμ•½ λΆ€μ„œμ§„ λ²ˆν˜Έκ°€ 숫자 쀑에 ν•˜λ‚˜λΌκ³  μžˆλ‹€λ©΄ (any) Falseλ₯Ό λ„£μ–΄μ£Όκ³ , μ•„λ‹ˆλΌλ©΄ Trueλ₯Ό cannel에 λ„£μ–΄μ€€λ‹€.

while 반볡문
cntκ°€ upDown의 횟수λ₯Ό λ„˜κΈ° μ „κΉŒμ§€λ§Œ λ°˜λ³΅ν•΄μ€€λ‹€.
( μ±„λ„μ˜ λ²”μœ„λ₯Ό λ„ˆλ¬΄ λ©€λ¦¬κΉŒμ§€ νƒμƒ‰ν•˜λŠ” 것을 막기 μœ„ν•΄, cntκ°€ upDown보닀 μž‘μ„ λ•Œλ§Œ λ°˜λ³΅ν•΄μ€€λ‹€. )

cntλŠ” λͺ©ν‘œ 채널과 κ°€μž₯ κ°€κΉŒμš΄ μ±„λ„κ³Όμ˜ 거리이닀.

μ²˜μŒμ—” 0으둜 μ‹œμž‘ν•˜μ—¬ λͺ©ν‘œ 채널이 λˆ„λ₯Ό 수 μžˆλŠ”μ§€ cannel을 μ‚¬μš©ν•΄μ„œ ν™•μΈν•˜κ³ , μ•„λ‹ˆλΌλ©΄ 거리 λ²”μœ„λ₯Ό +1 ν•΄μ€€λ‹€.

λͺ©ν‘œ μ±„λ„μ—μ„œ cnt 만큼 떨어진 채널이 λˆ„λ₯Ό 수 μžˆλ‹€λ©΄, ν•΄λ‹Ή 거리 cnt에 채널을 λˆ„λ₯΄κΈ° μœ„ν•΄ ν•„μš”ν•œ λ²„νŠΌ 횟수λ₯Ό 더해쀀닀.

( target-cnt와 target+cnt )κ°€ λ™μ‹œμ— 성립할 κ²½μš°μ—λŠ” target-cntκ°€ λ²„νŠΌμ„ 더 적게 λˆ„λ₯Ό κ°€λŠ₯성이 있기 λ•Œλ¬Έμ— target-cntλ₯Ό νŒλ‹¨ν•˜λŠ” 쑰건문을 λ¨Όμ € 써주어야 ν•œλ‹€.

λ§ˆμ§€λ§‰μœΌλ‘œ 2가지 방법 μ€‘μ—μ„œ μž‘μ€ 수λ₯Ό 좜λ ₯ν•΄μ€€λ‹€.

print(min(cnt, upDown))
전체 μ½”λ“œ
input = open(0).readline

cannel = []

target = int(input())

limit = target*2 + 100

upDown = target - 100 if target > 100 else 100 - target

cnt = 0

if (brokenCnt := int(input())):
    broken = list(map(str,input().split()))

    for i in range(limit):
        cannel.append(False if any(b in str(i) for b in broken) else True)

    while cnt < upDown:
        
        if (cannel[target-cnt] if target>=cnt else False):
            cnt += len(str(target-cnt))
            break
        
        if (cannel[target+cnt] if target+cnt < limit else False):
            cnt += len(str(target+cnt))
            break
        
        cnt += 1
else:
    cnt += len(str(target))
            
print(min(cnt, upDown))

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

이 문제λ₯Ό ν’€λ©΄μ„œ μ°Έκ³ ν•œ μˆ˜λ§Žμ€ λ°˜λ‘€ μΌ€μ΄μŠ€

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

예-전에 μ œκ°€ 짰던 Cμ½”λ“œλ₯Ό 파이썬으둜 λ³€ν™˜ν•˜λ‹ˆκΉŒ 톡과 μ‹œκ°„μ΄ μ €μ„Έμƒμ΄μ–΄μ„œ κ°–λ‹€ λ²„λ ΈμŠ΅λ‹ˆλ‹€ γ…Žγ…Ž...

μ•½κ°„ 생각을 λ°”κΏ”μ„œ
μ• μ΄ˆλΆ€ν„° 망가진 λ²„νŠΌμ„ μ œμ™Έν•˜κ³ , 망가지지 μ•Šμ€ λ²„νŠΌλ“€λ§Œ 가지고 λ§Œλ“€ 수 μžˆλŠ” 1자리~6자리 숫자λ₯Ό μž¬κ·€λ₯Ό 톡해 λ§Œλ“€μ–΄μ„œ κ·Έ κ²½μš°μ— λŒ€ν•΄μ„œ 횟수λ₯Ό κ°±μ‹ ν•˜λŠ” ν˜•μ‹μ˜ μ½”λ“œλ₯Ό μ§°μŠ΅λ‹ˆλ‹€.

input = open(0).readline

N = int(input())
if (M := int(input())):
    broken_btns = set(map(int, input().split()))
    if M == 10:
        print(abs(100 - N))
    else:
        ans = abs(100 - N)
        def backtracking(num: str = ''):
            global ans
            
            for btn in range(10):
                if btn in broken_btns:
                    continue
                
                tmp_num: str = num + str(btn)
                ans = min(ans, abs(N - int(tmp_num)) + len(tmp_num))
                
                if len(tmp_num) < 6:
                    backtracking(tmp_num)
                    
        backtracking()
        print(ans)
else:
    print(min(abs(100 - N), len(str(N))))
image 음... μ—¬μ „νžˆ 많이 λŠλ¦¬κ΅°μš”

Copy link
Collaborator

@Dolchae Dolchae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

쑰건문을 μ΄λ ‡κ²Œ μ€„μ—¬μ„œ μ‚¬μš©ν•˜λ‹ˆκΉŒ μ½”λ“œκ°€ 정말 κΉ”λ”ν•΄λ³΄μ΄λ„€μš”!πŸ‘ μˆ˜κ³ ν•˜μƒΈμŠ΅λ‹ˆλ‹€πŸ˜ŠπŸ˜Š

Copy link
Member

@xxubin04 xxubin04 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

브루트포슀 = λ…Έκ°€λ‹€ λ„ˆλ¬΄ μ›ƒκΈ°λ„€μš”πŸ˜
μ €λ²ˆμ— 브루트포슀 μ•Œκ³ λ¦¬μ¦˜ λ„μ „ν–ˆλ‹€κ°€ ν¬κΈ°ν–ˆμ—ˆλŠ”λ°
μžμ„Έν•˜κ²Œ PR μ¨μ£Όμ…”μ„œ μ‰½κ²Œ 이해할 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€!
문제 ν’€λ‹€κ°€ λ°˜λ‘€ μΌ€μ΄μŠ€ μ μš©ν•΄λ³΄κ³  끝내 λ§žμΆ”λ©΄ μ§„μ§œ 기뢄쒋은 것 κ°™μŠ΅λ‹ˆλ‹€...
수고 λ§ŽμœΌμ…¨μŠ΅λ‹ˆλ‹€!!

@gjsk132 gjsk132 merged commit 933eec4 into main Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants