递归求全排列(dfs)
一张图看懂。
- 交换
- 递归
- 回溯
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <functional>
#include <stack>
#include <ctime>
#include <cstdlib>
//#pragma comment (linker, "/STACK:256000000")
using namespace std;
#define db(x) cout<<x<<endl
typedef long long ll;
const int N = 200 + 10;
const int INF = 0x3f3f3f;
void print(int x[])
{
for(int i = 0; i < 4; i++){
printf("%d ", x[i]);
}
cout << endl;
}
void f(int x[], int k)
{
int i, t;
if (k >= 4)
{
print(x);
return;
}
for (i = k; i < 4; i++)
{
{
t = x[k];
x[k] = x[i];
x[i] = t;
}
f(x, k + 1);
{
t = x[k];
x[k] = x[i];
x[i] = t;
}
}
}
int main()
{
int x[] = {1, 2, 3, 4};
f(x, 0);
return 0;
}