[ 알고리즘 ]/Back Tracking

[백준] 15649. N과 M (1)

황찌옹 2022. 6. 30. 17:16

 

📌문제

https://www.acmicpc.net/problem/15649

 

🎖️난이도

 

✔️풀이

# sol1) back tracking (recursion)
N, M = map(int, input().split())
result = list()

def func():
    if len(result) == M:
        print(' '.join(map(str, result)))
        return
    for i in range(1, N+1):
        if i not in result:
            result.append(i)
            func()
            result.pop()

func()

 

# sol2) itertools - permutations (순열)
from itertools import permutations

n, m = map(int, input().split())

for i in permutations(range(1, n+1), m):
    # int로 출력
    for j in i:
        print(j, end=' ')
    print()

 

# sol3
from itertools import permutations

n, m = map(int, input().split())

for i in permutations(range(1, n+1), m):
    # str로 출력
    print(' '.join(map(str, i)))