其实就是全排列,用深搜做。
对于每一位也就是path[x] ,我们只需枚举一个i,
枚举i是前面几位没用过的,所以我们用一个数组记录。
但是有不止一种排列方法,这个数在其他数列也要用到,
所以,递归至下一位时,需要恢复现场。
#include <bits/stdc++.h>
using namespace std ;
const int N = 1e2 + 5 ;
int n ;
bool st[N] ;
int path[N] ;
void dfs ( int x ) {
if ( x == n ) {
for ( int i = 0 ; i < n ; i ++ ) cout << path[i] << ' ' ;
cout << endl ;
return ;
}
for ( int i = 1 ; i <= n ; i ++ )
if ( !st[i] ) {
path[x] = i ;
st[i] = true ;
dfs ( x + 1 ) ;
st[i] = false ;
}
}
int main ( ) {
cin >> n ;
dfs ( 0 ) ;
return 0 ;
}