AcWing 93. 递归实现组合型枚举
原题链接
简单
作者:
Galaxy_06
,
2024-03-25 20:44:49
,
所有人可见
,
阅读 2
递归需要考虑的事情
- 如何从父到子?
- 边界是哪里? 要考虑父过渡到子的方式, 否则会出现大一小一这种情况
- 如何从兄到弟?
- 如何剪枝?
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 30;
int st[N];
int m, n;
void dfs(int h, int e){
if(h >= m){
for(int i = 1; i <= m; i++) printf("%d ", st[i]);
puts("");
return;
}
for(int i = e + 1; n - i < m - h - 1, i <= n; i++){
st[h + 1] = i;
dfs(h + 1, i);
st[h + 1] = 0;
}
}
int main(){
cin >> n >> m;
dfs(0, 0);
return 0;
}