C++ 代码
#include<iostream>
using namespace std;
int n;
int path[20];
bool st[20];
void dfs(int u)
{
if(u == n)
{
for(int i = 0; i < n; i ++ ) cout << path[i] << " ";
cout << endl;
}
for(int i = 1; i <= n; i ++ )
{
if(!st[i])
{
st[i] = true;
path[u] = i;
dfs(u+1);
st[i] = false;
}
}
}
int main()
{
cin >> n;
dfs(0);
return 0;
}