深搜板子加一点点修改,适合初学者体会深搜,具体看代码
题目链接
ACcode
#include<bits/stdc++.h>
using namespace std;
int a, b;
bitset<50>vis;//剪枝
int d[50];
void dfs(int x) {
if (x == b + 1) {
for (int i = 1;i <= b;i++)cout << setw(3) << d[i];
cout << '\n';
return;
}
for (int i = x;i <= a;i++) {
if (vis[i])continue;
if (d[x - 1] > i)continue;//题目要求必须从小到大输出
vis[i] = 1;
d[x] = i;
dfs(x + 1);
vis[i] = 0;
d[x] = 0;
}
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> a >> b;
dfs(1);
return 0;
}