# 라이브러리를 이용해 순열을 구함
from itertools import permutations
N, M = map(int, input().split())
ls = list(permutations(range(1, N + 1), M))
for i in ls:
for j in i:
print(j, end=' ')
# dfs(깊이 우선 탐색)
N, M = map(int, input().split())
s = [] # 수열이 담길 리스트
visited = [False] * (N+1) # 중복제거를 위한 체크용 리스트
def dfs():
if len(s) == M:
print(' '.join((map(str, s))))
return
for i in range(1, N+1):
if visited[i]:
continue
visited[i] = True
s.append(i)
dfs()
s.pop()
visited[i] = False
dfs()
https://www.acmicpc.net/problem/15649
15649번: N과 M (1)
한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해
www.acmicpc.net
'study > Algorithm' 카테고리의 다른 글
[백준] 파이썬 15650 : N과 M (2) (0) | 2024.03.30 |
---|---|
[백준] 파이썬 24313 : 알고리즘 수업 - 점근적 표기 1 (0) | 2024.03.30 |
[백준] 파이썬 10816 : 숫자 카드 2 (0) | 2024.03.30 |
[백준] 파이썬 24267 : 알고리즘의 수행시간 6 (0) | 2024.03.30 |
[백준] 파이썬 24265 : 알고리즘 수업 - 알고리즘의 수행 시간 4 (0) | 2024.03.30 |