[Algorithm] 중복순열 구하기 (DFS)

myeonji·2022년 2월 28일
0

Algorithm

목록 보기
54/89

def DFS(L):
    global cnt
    if L == m:
        for j in range(m):
            print(res[j], end=' ')
        print()
        cnt += 1

    else:
        for i in range(1, n+1):
            res[L] = i
            DFS(L+1)


n, m = map(int, sys.stdin.readline().split())
res = [0]*m
cnt = 0
DFS(0)
print(cnt)

res 는 m 만큼 만들었기 때문에 동적이여서 if L == m: 문에서 for 문으로 m 길이만큼 res 출력해야 한다.

0개의 댓글