dfs实现全排列板子

#include<iostream>
#include<algorithm>
using namespace std;
const int N=10010;
bool st[N];
int res[N];
int n;
void dfs(int x)
{
    if(x==n+1)
    {
    for(int i=1;i<=n;i++)
    cout<<res[i]<<" ";
    cout<<endl;
    return ;
    }
    for(int i=1;i<=n;i++)
    {
        if(!st[i])
        {
            res[x]=i;
            st[i]=true;
            dfs(x+1);
            st[i]=false;
        }
        
    }
    
    
}

int main(){
    cin>>n;
    dfs(1);
    return 0;
}

 

posted @ 2022-04-07 14:15  山海自有归期  阅读(36)  评论(0编辑  收藏  举报