[Algorithm] ๐Ÿšฉ๋™์ „ ๋ฐ”๊ฟ”์ฃผ๊ธฐ (DFS) (DP๋กœ๋„ ํ’€๊ธฐ)

myeonjiยท2022๋…„ 3์›” 14์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
81/89

<DFS๋กœ ํ’€ ๊ฒฝ์šฐ, ์ƒํƒœํŠธ๋ฆฌ>

def DFS(L, s):
    global cnt
    if s > t:  # ์ปท ์—ฃ์ง€
        return
    if L == k:
        if s == t:
            cnt += 1
    else:
        for i in range(coin[L][1]+1):
            DFS(L+1, s+(i*coin[L][0]))


if __name__ == '__main__':
    t = int(input())  # ์ง€ํ์˜ ๊ธˆ์•ก
    k = int(input())  # ๋™์ „์˜ ๊ฐ€์ง€ ์ˆ˜
    coin = []
    for _ in range(k):
        coin.append(list(map(int, input().split())))
    cnt = 0
    DFS(0, 0)
    print(cnt)

<DP๋กœ ํ’€๊ธฐ>

t=int(input())
n=int(input())
dy=[0]*(t+1)
dy[0]=1
for i in range(n):
    a, b=map(int, input().split())
    for j in range(t, 0, -1):
        for k in range(1, b+1):
            if(j-a*k<0): break
            dy[j]+=dy[j-a*k]
print(dy[t])
profile
๐Ÿ“š

0๊ฐœ์˜ ๋Œ“๊ธ€