인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

chj789510660's profile image
chj789510660

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

2. Vacation (DFS)

왜 2, 5, 6, 7이 출력 되는지 모르겠습니다

Written on

·

182

0

 

def DFS(v, sum):
global res
if v > a + 1:
return
elif v == a + 1:
if sum > res:
res = sum

else:
for i in range(v, a + 1):
if cnt[i] == 0:
cnt[i] = 1
DFS(v + b[i], sum + c[i])
cnt[i] = 0
DFS(v + 1, sum)
cnt[i] = 0

if __name__=="__main__":
a = int(input())
b = [0] * (a + 1)
c = [0] * (a + 1)
for i in range(1, a + 1):
d, e = map(int, input().split())
b[i] = d
c[i] = e
res = -2147000000
cnt = [0] * (a + 1)
DFS(1, 0)
print(res)

2, 5, 6, 7은 DFS(v + b[i], sum + c[i]) 이 부분 때문에 출력이 안될거라 생각했는데 나왔어요
어디서 잘못된 걸 까요?
코테 준비 같이 해요! python

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

재귀호출을 

DFS(i + b[i], sum + c[i])

해야 맞습니다.

chj789510660's profile image
chj789510660

asked

Ask a question