Part5.10_완전탐색_부분집합 구하기(DFS)_조합구하기

Eugenius1st·2022년 1월 26일
0

Python_algorithm

목록 보기
38/83

조합 구하기

내가 생각한 코드

import sys
sys.stdin = open("input.txt", "rt")

def DFS(L):
    global cnt
    global stdNum
    if L == m:
        for x in ch:
            print (x, end=" ")
        cnt+=1
        print()


    else:
        if L>0:
            stdNum = ch[L-1]
        for i in range(stdNum, n+1):
            if level[i] == 0:
                level[i] = 1
                ch[L] = i
                DFS(L+1)
                level[i] = 0;

                 

if __name__ == "__main__":
    stdNum = 1
    cnt = 0
    n, m = map(int,input().split()) # 4 2
    level = [0] *(n+1) #4
    ch = [0] * m #2
    DFS(0)
    print(cnt)
    ```
    
   정말 오래 걸렸디...
   근데 알고보니 그냥 기준 값 하나만 주면 되었던 것을...
   과연 선생님은 얼마나 더 쉽게 하셨을까?
   
   ## 선생님 코드
   ```python
   import sys
sys.stdin = open("input.txt", "rt")

def DFS(L, s):
    global cnt
    if L == m:
        for j in range(L):
            print(res[j], end=' ')
        cnt+=1
        print()
    else:
        for i in range(s, n): #n은 4
            res[L] = i
            DFS(L+1, i+1) #레벨 1 증가, 뻗는 가지 1 증가
            
                 

if __name__ == "__main__":
    n, m = map(int,input().split()) # 4 2
    res = [0] * (n+1) #2
    cnt = 0
    DFS(0, 1)

그렇지.. 생각해보면 인자로 줄 수 있는 수가 있었잖아..
왜 응용이 안되는거니 너는? ㅌㅋㅋㅋ
굳이 변수를 만들었어야만
속이 시원했 냐?!

profile
최강 프론트엔드 개발자가 되고싶은 안유진 입니다

0개의 댓글