p1706 全排列

#include <iostream>
#include <cstdio>
#include<iomanip>
using namespace std;
int b[9];
int ans;
int n;
int panduan(int a[], int n)//1不在,0在
{
    bool x = 1;
    for (int i = 1; i <= sizeof(a); i++)
    {
        if (a[i] == n)
        {
            x = 0;
            return 0;
        }
    }
    return 1;
}
void rec(int depth)
{//把这个过程和树对应起来
    if (depth == n + 1)//结束条件
    {
        for (int i = 1; i < depth; i++)
        {
            cout <<setw(5)<< b[i];
        }
        cout << endl;
        
        return;
    }
    for (int i = 1; i <= n; i++)//dfs时一开始就有n种选择的情况
    {

        if (panduan(b, i))//去重
        {
            b[depth] = i;//dfs(前)特别注意这里对应理解树!!!
            rec(depth + 1);//   特别注意这里对应理解树!!!
            b[depth] = 0;//这里的回溯必不可少(dfs后)
        }
    }
}
int main()
{
    cin >> n;
    rec(1);
    return 0;
}

       
这篇文章,是又一个故事的结束...
lazy's story is continuing.
原文地址:https://www.cnblogs.com/Hello-world-hello-lazy/p/14380495.html