#include<iostream>//注意本题是 字典排序
#include<cstdio>//时间是 n!
#include<cstring>
#include<algorithm>
using namespace std;
const int N=10;
int n;
int state[N];// 0 表示还没放数,1~n表示放了哪个数
bool used[N]; // true表示用过,false表示还未用过
void dfs(int u)
{
if(u>n) // 边界
{
for(int i=1;i<=n;i++){
// 打印方案
printf("%d ", state[i]);
}
puts("");
return ;
}
// 依次枚举每个分支,即当前位置可以填哪些数
for(int i=1;i<=n;i++){
if(!used[i]){
state[u]=i;
used[i]=true;
dfs(u+1);
//恢复现场
state[u]=0;
used[i]=false;
}
}
}
int main()
{
scanf("%d",&n);
dfs(1);
return 0;
}
acwing 94 递归实现排列型枚举(递归)
猜你喜欢
转载自blog.csdn.net/qq_47874905/article/details/114841902
今日推荐
周排行