'commit'
This commit is contained in:
38
TangDou/Test/dfs_TangDou.cpp
Normal file
38
TangDou/Test/dfs_TangDou.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user