Algorithm/SWEA

[SWEA - D3] 5215. ํ–„๋ฒ„๊ฑฐ ๋‹ค์ด์–ดํŠธ

hello_u 2023. 5. 10. 22:52

 

 

๋‚˜์˜ ์ฝ”๋“œ 

def DFS(L,score,kcal):
    global res 
    if kcal > k:
        return
    if score > res:
        res = score 
    if L == n:
        return
        
    s,c = arr[L]
    DFS(L+1,score+s, kcal+c)
    DFS(L+1,score,kcal)
    
T = int(input())
for t in range(1,T+1):
    n,k = map(int,input().split()) 
    arr = []
    res = 0
    for _ in range(n):
        a,b = map(int,input().split()) 
        arr.append((a,b))
    DFS(0,0,0)
    print("#"+str(t) , res )

 

DFS ๋ฅผ ์ด์šฉํ•œ ํ’€์ด 

 

ํ–„๋ฒ„๊ฑฐ ์žฌ๋ฃŒ์ˆ˜(n), ๋„˜์ง€ ๋ง์•„์•ผ ํ•˜๋Š” ์นผ๋กœ๋ฆฌ์ˆ˜(k)์„ ์ž…๋ ฅ๋ฐ›๊ณ 

๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ง›๊ณผ ์นผ๋กœ๋ฆฌ๋ฅผ ์ €์žฅํ•œ ํ›„ 

 

์นผ๋กœ๋ฆฌ๊ฐ€ ๋„˜์–ด๊ฐ€๋ฉด ํƒ์ƒ‰์„ ์ˆ˜ํ–‰X

๊ธฐ์กด์— ์ €์žฅ๋œ ์ตœ๋Œ€ ๋ง›๋ณด๋‹ค ํฌ๋ฉด ๊ฐฑ์‹  

๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ค๋Œ๋ฉด return

 

์žฌ๋ฃŒ๋ฅผ ์ผ์„ ๋•Œ, ์•ˆ ์ผ์„ ๋•Œ ๋ฅผ ๊ฐ๊ฐ ํƒ์ƒ‰