本子君

深度优先搜索 全排列

#include <bits/stdc++.h>
using namespace std;
int a[10],book[10],n;//全局变量未赋值为0 
void dfs(int step)
    {
        if(step==n+1)
        {
            for(int i=1;i<=n;i++)
            cout<<a[i];
            cout<<endl;
            return; 
        }
        for(int i=1;i<=n;i++)
        {
            if(book[i]==0)
            {
                a[step]=i;
                book[i]=1;
                dfs(step+1);
                book[i]=0;
            }
        }
        return;
    }

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

 

posted on 2019-02-28 22:43  本子君  阅读(268)  评论(0编辑  收藏  举报

导航