当前位置:网站首页>Full Permutation (depth first, permutation tree)
Full Permutation (depth first, permutation tree)
2022-07-27 08:41:00 【Madness makes freedom】
1) Depth-first search
/**
1) Depth-first search
*/
#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); // Fill first x Number of sign positions
int temp_max=0,temp_min=0;
int main()
{
int n;
printf(" Enter a positive integer :\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(" The elapsed time %.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) Permutation tree
/**
2) Permutation tree
*/
#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(" Enter a positive integer :\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(" The elapsed time %.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]);
}
}边栏推荐
猜你喜欢

Explain cache consistency and memory barrier

Include error in vs Code (new header file)

Using ecological power, opengauss breaks through the performance bottleneck

Functions and arrow functions

How to upload qiniu cloud

带宽 与 货币

Solution of database migration error

E. Split into two sets

View 的滑动冲突

User management - restrictions
随机推荐
第2章 前台数据展现
无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。
Solution of database migration error
Realize SKU management in the background
Sliding conflict of view
【渗透测试工具分享】【dnslog服务器搭建指导】
Background order management
openGauss之TryMe初体验
3428. Put apples
Installation and use of Supervisor
JS rotation chart
P7 Day1 get to know the flask framework
Functions and arrow functions
What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?
JS advanced knowledge - function
View 的滑动冲突
JS basic knowledge - daily learning summary ①
4274. Suffix expression
Solution to the program design of the sequence structure of one book (Chapter 1)
Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?