首页 > 其他 > 详细

p1706 全排列

时间:2021-02-06 10:48:23      阅读:36      评论:0      收藏:0      [点我收藏+]
#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;
}

       

p1706 全排列

原文:https://www.cnblogs.com/Hello-world-hello-lazy/p/14380495.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!