状压 Dp 基础题。
代码还算精简的了
#include <bits/stdc++.h>
using namespace std;
const int N = 26, M = 1 << 21;
int n, m, a, tot;
vector<short> ans[M];
bitset<N> Map;
void work()
{
tot ++;
for (int i = 0; i <= n; i ++ )
if (Map[i]) ans[tot].push_back(i + 1);
}
bool cmp(vector<short> a, vector<short> b)
{
for (int i = 0; i < m; i ++ )
{
if (a[i] < b[i]) return true;
if (a[i] > b[i]) return false;
}
}
int main()
{
scanf("%d %d", &n, &m);
for (int a = 0; a < (1 << n); a ++, Map.reset())
{
Map |= a;
if (Map.count() != m) continue;
work();
}
sort(ans, ans + tot - 1);
for (int i = 1; i <= tot; i ++ ) {
for (int j = 0; j < m; j ++ )
printf("%d ", ans[i][j]);
puts("");
}
return 0;
}