부분집합 구하기

이기훈·2021년 5월 10일

pythonAlgorithm

목록 보기
1/1

파이썬 코딩테스트 대비

완전탐색

부분집합 구하기(DFS)

n = 3
def Algorithim(level):
	if level==n+1:
    	for i in range(1, n+1):
        	check[i]=1:
            	print(i, end=" ")
        print()
    else:
    	check[level]=1
        print('앞', level)
        Algorithm(level)
        check[level]=0
        print('뒤', level)
        Algorithm(level)
check=[0]*(n+1)
Algorithm(1)
  • level이 1씩 증가하면서 check=[0,0,0,0]을 0에서 1로 변경 시킨다.
    위의 Algorithm 함수가 먼저 실행 (pop)된다.
    위의 Algorithm 함수가 모두 pop이 되고 나서
    아래의 Algorithm이 실행 된다.
    아래의 Algorithm의 level이 모두 채워질때까지 실행이 되며
    결과는 아래와 같이 나온다.
    결과물

  • 앞, 뒤 print를 뺀 결과물결과

profile
Beyond Code

0개의 댓글