'commit'
This commit is contained in:
44
TangDou/Dfs/DFS10_PaiLieShuChu.cpp
Normal file
44
TangDou/Dfs/DFS10_PaiLieShuChu.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 110;
|
||||
vector<int> a;
|
||||
int st[N];
|
||||
int n, r;
|
||||
/**
|
||||
测试用例:
|
||||
3 2
|
||||
|
||||
1 2
|
||||
1 3
|
||||
2 1
|
||||
2 3
|
||||
3 1
|
||||
3 2
|
||||
*/
|
||||
void dfs(int step, int m) {
|
||||
if (m == r) {
|
||||
for (int i = 0; i < a.size(); i++) printf("%d ", a[i]);
|
||||
printf("\n");
|
||||
return;
|
||||
}
|
||||
if (step == n + 1)return;
|
||||
//每个数字都可尝试放进当前箱子
|
||||
for (int i = 1; i <= n; i++) {
|
||||
//如果没有用过
|
||||
if (!st[i]) {
|
||||
//选择
|
||||
a.push_back(i);
|
||||
st[i] = true;
|
||||
dfs(step + 1, m + 1);
|
||||
st[i] = false;
|
||||
a.pop_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> n >> r;
|
||||
dfs(1, 0);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user