https://www.acmicpc.net/problem/15651
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
/******** 전역변수 ********/
int n, m;
int arr[9] = { 0 };
/******** 함 수 ********/
void dfs(int num) { // DFS : 깊이 우선 탐색
if (num == m) { // 출력해야 할 만큼의 수(m) 가 되었을 때 출력
for (int i = 0; i < m; i++) {
cout << arr[i] << " ";
}
cout << endl;
return;
}
// 방문 검사를 하지 않고 모두 저장
for (int i = 1; i <= n; i++) {
arr[num] = i;
dfs(num + 1);
}
}
int main(void) {
/******** C++ INIT ********/
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
/******** 변 수 ********/
/******** 구 현 ********/
cin >> n >> m;
dfs(0);
}
'알고리즘' 카테고리의 다른 글
C++ 알고리즘 - 24416 알고리즘 수업 - 피보나치 수 1 (0) | 2025.01.21 |
---|---|
C++ 알고리즘 - 15652 N과 M (4) (백트래킹) (0) | 2025.01.21 |
C++ 알고리즘 - 4779 칸토어 집합 (0) | 2025.01.21 |
C++ 알고리즘 - 2776 암기왕 (0) | 2025.01.20 |
C++ 알고리즘 - 15650 N과 M (2) (백트래킹) (0) | 2025.01.20 |