Files
python/TangDou/LanQiaoBei/LanQiao12ShengSai/BC4_dfs_Prepare_2.cpp

34 lines
731 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
// https://www.acwing.com/solution/content/34401/
using namespace std;
const int N = 20;
int a[N];
bool st[N];
int n = 3;
void dfs(int step) {
if (step == n + 1) {
for (int i = 1; i <= n; i++)printf("%d ", a[i]);
printf("\n");
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;
}