코딩테스트/SW Expert Academy

swea. D3 - 5215. 햄버거 다이어트 Python

1son 2023. 11. 4. 21:56

🔗문제 링크 

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&contestProbId=AWT-lPB6dHUDFAVT&categoryId=AWT-lPB6dHUDFAVT&categoryType=CODE&problemTitle=&orderBy=RECOMMEND_COUNT&selectCodeLang=ALL&select-1=3&pageSize=10&pageIndex=1

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

👩‍💻참고 블로그 

 

[SWEA/DFS-BFS] 5215[D3]: 햄버거 다이어트 - 파이썬

문제 SWEA-5215. 햄버거 다이어트 SW Expert Academy SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요! swexpertacademy.com 풀이 DFS를 이용해 풀이하였음. 햄버거 재료수(N), 넘지 말

whitehairhan.tistory.com

 

 

💡코드 

# swea D3 - 5215. 햄버거 다이어트

#sTaste -> sum Taste
#sKcal -> sum Kcal
def dfs(index, sTaste, sKcal):
    global maxTaste

    #총 칼로리를 넘으면 리턴
    if sKcal > limit:
        print("칼로리가 limit 넘는 값을 넘겨줬잖아! 리턴할거야")
        return

    if maxTaste < sTaste:
        print("maxTaste 갱신!"+str(sTaste))
        maxTaste = sTaste

    if index == n:
        print("index와 n이 같은 수여서 return")
        return

    taste, kcal = arr[index]

    print("dfs("+str(index+1)+","+str(sTaste+taste)+","+str(sKcal+kcal))
    dfs(index+1, sTaste+taste,sKcal+kcal)
    print("돌아왓져 dfs("+str(index + 1)+","+str(sTaste)+","+ str(sKcal))
    dfs(index+1, sTaste,sKcal)

t=int(input())
for tc in range(1,t+1):
    n,limit = map(int,input().split())
    arr=[list(map(int,input().split())) for _ in range(n)]

    maxTaste = 0
    dfs(0,0,0)

    print(f'#{tc} {maxTaste}')

 

 

def dfs(index, sTaste, sKcal):
    global maxTaste

    #총 칼로리를 넘으면 리턴
    if sKcal > limit:
        return

    if maxTaste < sTaste:
        maxTaste = sTaste

    if index == n:
        return

    taste, kcal = arr[index]

    dfs(index+1, sTaste+taste,sKcal+kcal)
    dfs(index+1, sTaste,sKcal)

t=int(input())
for tc in range(1,t+1):
    n,limit = map(int,input().split())
    arr=[list(map(int,input().split())) for _ in range(n)]

    maxTaste = 0
    dfs(0,0,0)

    print(f'#{tc} {maxTaste}')