자료구조와 알고리즘/개인적인 코딩테스트 관련 풀이

[완전탐색][트리] - 조합 구하기(DFS)

얄루몬 2022. 7. 25. 18:42

def DFS(L,s):
    global cnt
    if L == m:
        for i in res:
            print(i, end =" ")
        print()
        cnt += 1

    else:
        for i in range(s, n+1):
            res[L] = i
            DFS(L+1, i+1)
        
            
if __name__ == "__main__":
    n, m = map(int,input().split())
    cnt = 0
    res = [0] * m
    DFS(0,1)
    print(cnt)
  • 앞에 들어간 숫자는 중복해서 나오면 안 되기 때문에 s로 +1 해주며 진행해준다.