私以为此乃DFS之基础
即DFS均以此拓展而来
#include <iostream>
#include <cstdio>
using namespace std;
const int MAXN = 1e5 + 10;
int N;
bool used[MAXN] = {0};
int arr[MAXN] = {0};
void dfs(int rit, int num)
{
if(num == N)
{
for(int i = 0; i < N; i++)
{
printf("%5d",arr[i]);
}
cout<<endl;
}
for(int i = 1; i <= N; i++)
{
if(!used[i])
{
used[i] = true;
arr[num] = i;
dfs(i, num + 1);
used[i] = false;
}
}
}
int main()
{
cin>>N;
dfs(0, 0);
return 0;
}