当前位置:网站首页>全排列(深度优先,排列树)
全排列(深度优先,排列树)
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]);
}
}边栏推荐
- Which securities company should I choose to open a stock account? What securities company is safer
- UVA340 Master-Mind Hints
- Leetcode 1275. Find out the winner of tic tac toe
- [cute new problem solving] sum of four numbers
- A - Trees on the level(树的层序遍历)
- 009 面试题 SQL语句各部分的执行顺序
- SBOM(Software Bill of Materials,软件物料清单)
- Leetcode 1296. 划分数组为连续数字的集合(已解决)
- Code runner for vs code, with more than 40million downloads! Support more than 50 languages
- 2020 ICPC Asia East Continent Final G. Prof. Pang‘s sequence 线段树/扫描线
猜你喜欢
随机推荐
UVA - 12096 The SetStack Computer
Several points to be analyzed in the domestic fpga/dsp/zynq scheme
How to read and save point cloud data with numpy
MySQL installation
07_服务注册与发现总结
Top domestic experts gathered in Guangzhou to discuss the safety application of health care data
SBOM (software bill of materials)
csrf防护机制
GYM103660H. Distance
Single channel 6Gsps 12 bit AD acquisition and single channel 6Gsps 16 bit Da (ad9176) output sub card based on vita57.1 standard
5-21 interceptor
论文阅读 TEMPORAL GRAPH NETWORKS FOR DEEP LEARNING ON DYNAMIC GRAPHS
JVM常用调优配置参数
Behind the high salary of programmers' operation and maintenance
[cute new problem solving] sum of four numbers
State machine exercise
UVA - 12096 The SetStack Computer
PKI: TLS handshake
揭开服务网格~Istio Service Mesh神秘的面纱
Code runner for vs code, with more than 40million downloads! Support more than 50 languages









