AcWing 92. 递归实现指数型枚举
原题链接
简单
作者:
uchar
,
2024-12-01 16:01:54
,
所有人可见
,
阅读 12
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
int st[16]; // 1是选, 2是不选, 0是暂时没考虑
int n;
vector<vector<int>> ways;
void dfs(int u)
{
if (u > n)
{
vector<int> way;
for (int i = 1; i <= n; i++)
if (st[i] == 1)
way.push_back(i);
ways.push_back(way);
return;
}
st[u] = 2;
dfs(u + 1);
st[u] = 0;
st[u] = 1;
dfs(u + 1);
st[u] = 0;
}
int main()
{
cin >> n;
dfs(1);
for (int i = 0; i < ways.size(); i++)
{
for (int j = 0; j < ways[i].size(); j++)
printf("%d ", ways[i][j]);
puts("");
}
return 0;
}