当前位置:网站首页>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]);
}
}边栏推荐
猜你喜欢

4274. 后缀表达式

Minio 安装与使用

借生态力量,openGauss突破性能瓶颈

All in one 1353 -- expression bracket matching (stack)

说透缓存一致性与内存屏障

MCDF top level verification scheme

MySQL Express

Vertical align cannot align the picture and text vertically

Day4 --- flask blueprint and rest ful

All in one 1251 - Fairy Island for medicine (breadth first search)
随机推荐
redis的string类型及bitmap
Flask's operations on model classes
Arm system call exception assembly
Creation and simple application of QPushButton button button
Query and association of flask to database
无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。
3311. 最长算术
It's better to be full than delicious; It's better to be drunk than drunk
Arm undefined instruction exception assembly
帮忙发几个招聘,有兴趣可以看看
Functions and arrow functions
Element display mode: block level, inline, inline block, nesting specification, display mode conversion
Blueprint class view method
Block, there is a gap between the block elements in the row
Massive data Xiao Feng: jointly build and govern opengauss root community and share a thriving new ecosystem
User management - restrictions
Flask project configuration
Set set
Is online account opening safe? Want to know how securities companies get preferential accounts?
Background image related applications - full, adaptive