Algorithm/백준
C++ 알고리즘 - 백준 15650 N과 M (3) (백트래킹)
마루설아
2025. 1. 21. 18:21
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);
}