样例的搜索方式
code
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10;
int n;
int p[N];
bool st[N];
void dfs(int u) {
if (u == n) { // 走到最后一个数 填满了
for (int i = 0; i < n; i ++ ) {
cout << p[i] << " ";
}
cout << "\n";
return ;
}
for (int i = 1; i <= n; i ++ ) {
if (!st[i]) { // 没遍历过i
p[u] = i; // 加到路径中
st[i] = 1; // 标记被算过
dfs(u + 1); // 递归到下一层
st[i] = 0; // 恢复现场
}
}
}
int main() {
cin >> n;
dfs(0);
return 0;
}