// 서로 다른 n개의 원소 중 m개의 원소를 중복없이 뽑는 경우(dfs, 백트래킹)
#include <bits/stdc++.h>
using namespace std;
int n, m, arr[9];
bool visited[9];
void dfs(int depth) {
if (depth == m) {
for (int i = 0; i < m; ++i) {
cout << arr[i] << ' ';
}
cout << '\n';
return;
}
for (int i = 1; i <= n; ++i) {
if (visited[i]) continue;
visited[i] = true;
arr[depth] = i;
dfs(depth + 1);
visited[i] = false;
}
}
void solve() {
cin >> n >> m;
dfs(0);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}'PS' 카테고리의 다른 글
| 백준 BOJ 15650 C++ [N과 M (2)] (0) | 2026.04.22 |
|---|---|
| 백준 BOJ 15649 C++ [N과 M (1)](2) (0) | 2026.04.22 |
| 백준 BOJ 2448 C++ 별 찍기 - 11 (0) | 2026.04.22 |
| 백준 BOJ 2447 C++ 별 찍기 - 10 (0) | 2026.04.22 |
| 백준 BOJ 1780 C++ 종이의 개수 (1) | 2026.04.22 |