본문 바로가기
Algorithm(CodeTree, Python)/Backtracking

[코드트리] 특정 조건에 맞게 k개 중에 1개를 n번 뽑기 Python

by kurooru 2023. 1. 30.
# k, n 입력
k, n = map(int, input().split())

# 함수들
# print_comb()
def print_comb():
    for num in comb:
        print(num, end=' ')
    print()

# make_comb(curr_idx)
def make_comb(curr_idx):
    # 종료조건
    if curr_idx == n + 1:
        # 출력
        print_comb()
        return
    
    # 숫자 넣어주기
    for i in range(1, k+1):
        # 3번째 자리 이상에서, 앞에 두개랑 같으면
        if curr_idx >= 3 and comb[-2] == i and comb[-1] == i:
            # skip
            continue
        # 이외에는
        else:
            comb.append(i)
            make_comb(curr_idx + 1)
            comb.pop()

# 설계
# comb
comb = []
# make_comb
make_comb(1)