๐Ÿ”‘ CS/์ฝ”๋”ฉํ…Œ์ŠคํŠธ

[๋ฐฑ์ค€ 9084๋ฒˆ] ๋™์ „ (python/ํŒŒ์ด์ฌ) + ์‹ค๋ฒ„ II ๋‹ฌ์„ฑ

๋ณต๋งŒ 2022. 10. 8. 19:25

๊ทธ๋ฆฌ๋””๋กœ ๋ชปํ‘ธ๋Š” ๋ฌธ์ œ์ด๊ธธ๋ž˜ DP์ธ์ค„ ์•Œ๊ณ  ์งฐ๋‹ค๊ฐ€

DP๋„ ์•„๋‹ˆ๊ตฌ๋‚˜ ์‹ถ์–ด์„œ DFS ๋น„์Šทํ•˜๊ฒŒ ํ’€์—ˆ๋‹ค๊ฐ€ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋– ์„œ

DFS + DP๋กœ ์„ฑ๊ณต

 

๐ŸŒผ ๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/9084

 

9084๋ฒˆ: ๋™์ „

์šฐ๋ฆฌ๋‚˜๋ผ ํ™”ํ๋‹จ์œ„, ํŠนํžˆ ๋™์ „์—๋Š” 1์›, 5์›, 10์›, 50์›, 100์›, 500์›์ด ์žˆ๋‹ค. ์ด ๋™์ „๋“ค๋กœ๋Š” ์ •์ˆ˜์˜ ๊ธˆ์•ก์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ทธ ๋ฐฉ๋ฒ•๋„ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 30์›์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š”

www.acmicpc.net

  • ํ‹ฐ์–ด:  ๊ณจ๋“œ V
  • ๋ถ„๋ฅ˜: ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ

 

 

โ— TRIAL 1.

๋”๋ณด๊ธฐ
์ฝ”๋“œ
def solution(N, coins, target):
    c = coins.pop() #largest
    
    if N == 1:
        if target % c == 0:
            ans = 1
        else:
            ans = 0
    else:        
        ans = 0
        for i in range(target//c + 1):
            ans += solution(N-1, coins, target-c*i)

    coins.append(c)
    return ans
    

T = int(input()) #num of test cases
for _ in range(T):
    N = int(input()) #num of coins
    coins = list(map(int, input().split())) #coins in ascending order
    M = int(input()) #target price

    print(solution(N, coins, M))

 

์ฝ”๋“œ ์„ค๋ช…

 

- ๊ฐ€์žฅ ํฐ coin๋‹จ์œ„๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜์— ๋Œ€ํ•ด ๊ฒ€์‚ฌ.

- ๋‚จ์€ ๊ธˆ์•ก์„ ๋‚˜๋จธ์ง€ coin์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ๋”ํ•œ ํ›„ return

 

ํ‹€๋ฆฐ ์›์ธ

 

์‹œ๊ฐ„์ดˆ๊ณผ. ๊ฐ™์€ coins๋กœ target์„ ๋งŒ๋“œ๋Š” ๊ณ„์‚ฐ์ด ์—ฌ๋Ÿฌ๋ฒˆ ๋“ฑ์žฅํ•˜๋ฏ€๋กœ ์—ฌ๊ธฐ์— ์ค‘๊ฐ„๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•˜๋Š” DP๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•จ.

 

๐Ÿ’ก Solution

์ฝ”๋“œ
from collections import defaultdict

def solution(d, N, coins, target):
    if target == 0:
        return 1
        
    c = coins.pop() #largest
    
    if N == 1:
        if target % c == 0:
            ans = 1
        else:
            ans = 0
    else:
        key = ' '.join(map(str, [target]+coins))
        if d[key] is None:
            ans = 0
            for i in range(target//c + 1):
                ans += solution(d, N-1, coins, target-c*i)
            d[key] = ans
        else:
            ans = d[key]

    coins.append(c)
    return ans
    

T = int(input()) #num of test cases
for _ in range(T):
    N = int(input()) #num of coins
    coins = list(map(int, input().split())) #coins in ascending order
    M = int(input()) #target price

    d = defaultdict(lambda: None)
    print(solution(d, N, coins, M))

 

์ฝ”๋“œ ์„ค๋ช…
  • DFS + DP
  • ๊ฐ€์žฅ ํฐ coin๋‹จ์œ„๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜์— ๋Œ€ํ•ด ๊ฒ€์‚ฌ.
  • ๋‚จ์€ ๊ธˆ์•ก์„ ๋‚˜๋จธ์ง€ coin์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ๋”ํ•œ ํ›„ return
  • ํŠน์ • coin๋“ค๋กœ ํŠน์ • target์„ ๋งŒ๋“œ๋Š” ๊ณ„์‚ฐ์ด ์—ฌ๋Ÿฌ๋ฒˆ ๋“ฑ์žฅํ•˜๋ฏ€๋กœ ์ด๋ฅผ ์ €์žฅํ•˜๋Š” dp๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
    • target + coin์„ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด dp์˜ key๋กœ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

 

๋” ๋‚˜์€ ํ’€์ด
  • ํ’€์ด1: DP bottom-up
    • coin์„ ํ•˜๋‚˜์”ฉ ์ˆœํšŒํ•˜๋ฉด์„œ,
      • 0๋ถ€ํ„ฐ target๊นŒ์ง€์˜ ๋ชจ๋“  ๊ธˆ์•ก์— ๋Œ€ํ•ด
        • ํ˜„์žฌ coin์„ ๋บ€ ๊ธˆ์•ก์— ๋Œ€ํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋”ํ•œ๋‹ค.

 

for c in coins:
	for i in range(c, target+1):
	    d[i] += d[i-coin]
        
print(d[target])

 

 

์‹ค๋ฒ„ II ๋‹ฌ์„ฑ ๐Ÿ˜Š

  •  
๋ฐ˜์‘ํ˜•