https://www.acmicpc.net/problem/10974
10974번: 모든 순열
N이 주어졌을 때, 1부터 N까지의 수로 이루어진 순열을 사전순으로 출력하는 프로그램을 작성하시오.
www.acmicpc.net
https://github.com/stellaluminary/Baekjoon
GitHub - stellaluminary/Baekjoon
Contribute to stellaluminary/Baekjoon development by creating an account on GitHub.
github.com
from itertools import permutations
n = int(input())
for i in permutations([s for s in range(1,n+1)]):
print(*i)
백트래킹
n = int(input())
s = [i for i in range(1,n+1)]
t = []
def dfs(depth):
if depth == n:
print(*t)
return
for i in range(n):
if s[i] not in t:
t.append(s[i])
dfs(depth+1)
t.pop()
dfs(0)