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

Inflearn Community Q&A

sarahong9705206595's profile image
sarahong9705206595

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

섹션 6) 부분집합 구하기

Written on

·

197

0

안녕하세요, 강사님. 강의 잘 듣고 있습니다!

def DFS(v)의 else문에서 이해가 가지 않는 부분이 있어 질문 드립니다.

import sys
sys.stdin=open("input.txt", "r")
def DFS(v):
    if v==n+1: #종료지점에 도달하면 출력! e.g. D(4)
        for i in range(1, n+1):
					 if ch[i]==1:
                print(i, end='')
        print() #line by line으로 출력
    else: #원소를 포함하냐 안 하냐(o,x)
        ch[v]=1 #o
        DFS(v+1)
        ch[v]=0 #x
        DFS(v+1)

if __name__=="__main__":
    n=int(input())
    ch=[0]*(n+1) #상태트리에서 원소를 포함하냐 안 하냐를 체크하는 변수 
    DFS(1) 

1, 2, 3

1, 2

까지 출력이 되는 것은 이해했습니다.

1, 2 까지 출력이 완료되고 나면 else문의 마지막 DFS(v+1)까지 다 돌아간 것인데 어떻게 back할 수 있는 건지 궁금합니다.

코테 준비 같이 해요! python

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

영상의 4분 50초부터 6분까지를 자세히 보세요. 그게 이해가 안가시면 앞 영상인 이진트리순회 영상을 다시 한 번 보시면 좋을 것 같습니다.

sarahong9705206595's profile image
sarahong9705206595

asked

Ask a question