https://www.acmicpc.net/problem/15652
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
/******** 전역변수 ********/
int n, m;
int arr[9] = { 0 };
/******** 함 수 ********/
void dfs(int idx, int num) { // DFS : 깊이 우선 탐색
if (num == m) { // 출력해야 할 만큼의 수(m) 가 되었을 때 출력
for (int i = 0; i < m; i++) {
cout << arr[i] << " ";
}
cout << endl;
return;
}
// i의 초기값을 idx 매개변수로 받는다 (지나온 숫자는 확인하지 않기 위함)
for (int i = idx; i <= n; i++) {
// 방문 검사를 하지 않음
arr[num] = i;
dfs(i, num + 1);
}
}
int main(void) {
/******** C++ INIT ********/
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
/******** 지역변수 ********/
/******** 구 현 ********/
cin >> n >> m;
dfs(1, 0);
}
'알고리즘' 카테고리의 다른 글
C++ 알고리즘 - 9184 신나는 함수 실행 (동적 계획법 / 재귀) (0) | 2025.01.22 |
---|---|
C++ 알고리즘 - 24416 알고리즘 수업 - 피보나치 수 1 (0) | 2025.01.21 |
C++ 알고리즘 - 15650 N과 M (3) (백트래킹) (0) | 2025.01.21 |
C++ 알고리즘 - 4779 칸토어 집합 (0) | 2025.01.21 |
C++ 알고리즘 - 2776 암기왕 (0) | 2025.01.20 |