1.使用STL的函数next_permutation(a, a+n)可以轻松生成全排列,操作过程如下:
#include <bits/stdc++.h>
using namespace std;
int a[] = {1, 2, 3, 4, 5, 6};
void print(int n)
{
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
}
int main()
{
do
{
print(6);
} while (next_permutation(a, a+6));
return 0;
}
2.DFS暴力枚举
#include <bits/stdc++.h>
using namespace std;
int a[6];
int n;
bool vis[6]; //该元素是否使用
void print()
{
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
}
void dfs(int val, int inx) //参数的含义表示将val放到下标索引为inx的位置上
{
a[inx] = val;
if (inx == n-1) {
print();
return ;
}
for (in