PS/BOJ

[백준] 15652. N과 M (4)(c++)

backend 개발자 지망생 2024. 4. 28. 22:58

https://www.acmicpc.net/problem/15652

 

백트래킹 조건으로 비내림차순을 정의해주면 된다.


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <string>
#include <cstring>
using namespace std;

using ll = long long;

int arr[8], visited[9];

int n, m;

void dfs(int idx) {
	if (idx == m) {
		for (int i = 0; i < m; i++) {
			cout << arr[i] << " ";
		}
		cout << '\n';
		return;
	}

	for (int i = 1; i <= n; i++) {
		if (i >= arr[idx - 1]) {
			arr[idx] = i;
			dfs(idx + 1);
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> n >> m;

	dfs(0);


	return 0;
}