🔗문제 링크
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}')
'코딩테스트 > SW Expert Academy' 카테고리의 다른 글
swea. D3 - 1215. [S/W 문제해결 기본] 3일차 - 회문1 python (0) | 2023.11.05 |
---|---|
swea. D3 - 1289. 원재의 메모리 복구하기 PYTHON (0) | 2023.11.05 |
swea. D3 - 2805. 농작물 수확하기 Python (0) | 2023.11.03 |
swea.D3 - 2806. N-Queen Python (0) | 2023.11.03 |
swea. D3 - 1208. [S/W 문제해결 기본] 1일차 - Flatten (0) | 2023.11.02 |