当前位置:网站首页>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]);
}
}边栏推荐
- Alibaba cloud international receipt message introduction and configuration process
- OPPO 自研大规模知识图谱及其在数智工程中的应用
- Solution to the program design of the sequence structure of one book (Chapter 1)
- redis 网络IO
- 4276. Good at C
- Background order management
- 带宽 与 货币
- 4274. 后缀表达式
- Openresty + keepalived 实现负载均衡 + IPV6 验证
- All in one 1353 -- expression bracket matching (stack)
猜你喜欢

After downloading URL loader and specifying the size of the image with limit, the image will not be displayed

Flutter 渲染机制——GPU线程渲染

Vertical align cannot align the picture and text vertically

Flask project configuration

Element display mode: block level, inline, inline block, nesting specification, display mode conversion

How to merge multiple columns in an excel table into one column

海关总署:这类产品暂停进口

Sliding conflict of view

How to permanently set source

Solution of database migration error
随机推荐
Functions and arrow functions
Implementation of registration function
3428. Put apples
Create a project to realize login and registration, generate JWT, and send verification code
Minio installation and use
Query and association of flask to database
【uni-app高级实战】手把手带你学习一个纯实战复杂项目的开发1/100
QPushButton 按钮的创建与简单应用
Luogu Taotao picks apples
VS Code中#include报错(新建的头文件)
Help send some recruitment. If you are interested, you can have a look
List delete collection elements
Introduction to depth first search (DFS)
【渗透测试工具分享】【dnslog服务器搭建指导】
海量数据肖枫:共建共治openGauss根社区,共享欣欣向荣新生态
4278. Summit
Flink1.15源码阅读flink-clients客户端执行流程(阅读较枯燥)
JS检测客户端软件是否安装
How to permanently set source
说透缓存一致性与内存屏障