Files
python/TangDou/LuoGuBook/P1157_dfs_vector.cpp

26 lines
593 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
int n, r;
//此答案,不吸氧,第8个测试点TLE,吸氧后通过。
void dfs(int step, vector<int> path, int m) {
if (step == n + 1) return;
if (m == r) {
for (int i = 0; i < r; i++) printf("%3d", path[i]);
printf("\n");
return;
}
//下一个数字选择上
vector<int> p1 = path;
p1.push_back(step + 1);
dfs(step + 1, p1, m + 1);
//下一个数字放弃掉
dfs(step + 1, path, m);
}
int main() {
cin >> n >> r;
dfs(0, {}, 0);
return 0;
}