본문 바로가기

알고리즘/백준 ~ 단계별 풀어보기

백준 15649번 "N과 M"

파이썬

import itertools
import sys

n , m  = map(int, sys.stdin.readline().split())

ans = itertools.permutations(range(1, n+1), m)
for i in ans:
    print(' '.join(map(str, i)))
import itertools
import sys

n , m  = map(int, sys.stdin.readline().split())

ans_list = []

def dfs():
    if len(ans_list) == m:
        print(" ".join(map(str, ans_list)))
    
    for i in range(1, n+1):
        if i not in ans_list:
            ans_list.append(i)
            dfs()
            ans_list.pop()
            
dfs()

# 참고 : https://jiwon-coding.tistory.com/21