You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
788 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 10;
int a[N];
bool st[N];
int n = 3;
//函数说明:填充第几个盒子
void dfs(int step) {
//如果全部填充完毕走到了最后虚拟的n+1号盒子面前就表示前面都完成了正确填充
if (step == n + 1) {
for (int i = 1; i <= n; i++) cout << a[i] << " ";
cout << endl;
return;
}
//每一个数字都可以用来填充
for (int i = 1; i <= n; i++)
//如果没有使用过
if (!st[i]) {
//放里
a[step] = i;
//标识已使用
st[i] = true;
//填充下一个
dfs(step + 1);
//标识未使用
st[i] = false;
}
}
int main() {
//开始填充第一个盒子
dfs(1);
return 0;
}