Files
python/TangDou/Test/dfs_TangDou.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

38 lines
788 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}