AcWing 94. 递归实现排列型枚举
原题链接
简单
作者:
鹏举_
,
2024-04-09 19:02:05
,
所有人可见
,
阅读 2
回溯解排列问题
#include <iostream>
#include <vector>
using namespace std;
const int N = 1e5+5;
int st[N];
int n;
vector<int> res;
// bool st[10];
void dfs(int u)
{
if(res.size()==n)
{
for(int i=0;i<n;i++)
{
cout << res[i] << " ";
}
cout << endl;
return ;
}
// return;
// res.push_back(u);
// st[u] = 1;
for(int i=1;i<=n;i++)
{
if(!st[i])
{
res.push_back(i);
st[i] = 1;
dfs(i);
st[i] = 0;
res.pop_back();
}
}
// res.pop_back();
}
int main()
{
// int n;
ios::sync_with_stdio(false);
cin >> n;
dfs(1);
}