#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 20;
int n;
bool st[N];//是否被选过
int path[N];
void dfs(int u)
{
if(u == n)
{
for(int i = 0;i < n;i ++)printf("%d ",path[i]);
cout << endl;
return;
}
for(int i = 1 ;i <= n;i ++)
{
if(!st[i])
{
path[u] = i;
st[i] = true;
dfs(u + 1);
//回溯
st[i] = false;
}
}
}
int main()
{
cin >> n;
dfs(0);
return 0;
}
AcWingDFS(842. 排列数字)
最新推荐文章于 2024-08-24 23:00:16 发布