当前位置:网站首页>全排列(深度优先,排列树)
全排列(深度优先,排列树)
2022-07-17 22:49:00 【疯疯癫癫才自由】
1)深度优先搜索
/**
1)深度优先搜索
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <algorithm>
using namespace std;
const int maxn=200;
int p[maxn],ans=0;
bool hashTable[maxn]={0};
void fullper(int x,int n); //填充第x号位的数
int temp_max=0,temp_min=0;
int main()
{
int n;
printf("输入一个正整数:\n");
scanf("%d",&n);
clock_t start,stop;
start=clock();
fullper(1,n);
cout << ans << endl;
cout << temp_max << endl;
cout << temp_min << endl;
stop =clock();
printf("运行时间 %.2f\n",(double)(stop-start)/CLK_TCK);
// int b[]={4,2,3,7};
// do
// {
// printf("%d %d %d %d\n",b[0],b[1],b[2],b[3]);
// }while(next_permutation(b,b+4));
// cout << "\nHello world!" << endl;
// printf("%d %d %d %d\n",b[0],b[1],b[2],b[3]);
return 0;
}
void fullper(int x,int n)
{
if(x==n+1)
{
++ans;
for(int i=1;i<=n;i++)
{
printf("%d",p[i]);
if(i<n) printf(" ");
}
printf("\n");
return;
}
for(int i=1;i<=n;i++)
{
++temp_max;
if(hashTable[i]==0)
{
++temp_min;
p[x]=i;
hashTable[i]=1;
fullper(x+1,n);
hashTable[i]=0;
}
}
}
2)排列树
/**
2)排列树
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <ctime>
using namespace std;
const int maxn=200;
int p[maxn],ans=0;
bool hashTable[maxn]={0};
void fullper(int x,int n);
int temp=0;
int main()
{
clock_t start,stop;
int n;
printf("输入一个正整数:\n");
scanf("%d",&n);
start=clock();
for(int i=1;i<=n;++i)
p[i]=i;
fullper(1,n);
cout << ans << endl;
cout << temp << endl;
stop =clock();
printf("运行时间 %.2f\n",(double)(stop-start)/CLK_TCK);
// int b[]={4,2,3,7};
// do
// {
// printf("%d %d %d %d\n",b[0],b[1],b[2],b[3]);
// }while(next_permutation(b,b+4));
// cout << "\nHello world!" << endl;
// printf("%d %d %d %d\n",b[0],b[1],b[2],b[3]);
return 0;
}
void fullper(int x,int n)
{
if(x==n+1)
{
++ans;
for(int i=1;i<=n;i++)
{
printf("%d",p[i]);
if(i<n) printf(" ");
}
printf("\n");
return;
}
for(int i=x;i<=n;i++)
{
++temp;
swap(p[i],p[x]);
fullper(x+1,n);
swap(p[i],p[x]);
}
}边栏推荐
- Read the paper: temporary graph networks for deep learning on dynamic graphs
- Setup的使用技巧
- Is it safe to buy funds in a securities account? I want to make a fixed investment in the fund
- MySQL CPU usage is soaring. How to locate who is occupying it
- Chang'an chain learning research - storage analysis wal mechanism
- Unveil the mystery of service grid istio service mesh
- Compositionapi component development paradigm
- 一次函数 T1744 963字符写法
- Cilium & Hubble
- Leetcode 1296. 划分数组为连续数字的集合(已解决)
猜你喜欢

Characteristics of DMA mode

Achieve the effect of software login account by authorizing wechat ~ ~ unfinished

ICML2022 | 幾何多模態對比錶示學習

Wechat applet 8 cloud function

FMC sub card: 4-channel 12bit 3.2g, 2-channel 12bit, 6.4g AD acquisition / 5G acquisition card /6g acquisition card

Zabbix实现对Redis的监控

UCAS. Deep learning Final review knowledge points summary notes

Classification of interrupts

Chang'an chain learning research - storage analysis wal mechanism

Face technology: the picture of unclear people is repaired into a high-quality and high-definition image framework (with source code download)
随机推荐
GYM103660L. Monster tower overall dichotomy
009 execution sequence of SQL statement of interview questions
csrf防护机制
2. MySQL introduction
44. Use orienmask for instance segmentation target detection, MNN deployment and ncnn deployment
How to read and save point cloud data with numpy
Comparaison de deux types de machines virtuelles
1. Basic concepts of DBMS
分布式事务总结
跨域与CORS
ICML2022 | 几何多模态对比表示学习
初试Dart,笔记
Istio XDS configuration generation implementation
UVA - 12096 The SetStack Computer
ObjectARX -- implementation of custom circle
06_服务调用Feign
Is it safe to buy funds in a securities account? I want to make a fixed investment in the fund
Comparison of two virtual machines
Chang'an chain learning research - storage analysis wal mechanism
Tips for using setup