作业比赛编号 : 100000608 - 《算法笔记》8.1小节——搜索专题->深度优先搜索(DFS)
Codeup新家 (hustoj.com)
递归解法:
#include<cstdio>
#include<algorithm>
#include<string>
#include<iostream>
#include<set>
#include<vector>
using namespace std;
set<int> num;
vector<int> temp;
int n, r;
void DFS(int nowR,int nowV)
{
int i;
if (nowR == r)
{
for (i = 0; i < temp.size(); i++)
{
printf("%d", temp[i]);
if (i != temp.size() - 1)printf(" ");
}
printf("\n");
return;
}
else if (nowV == n+1)return;
temp.push_back(nowV);
DFS(nowR + 1, nowV + 1);
temp.pop_back();
DFS(nowR, nowV + 1);
}
int main()
{
scanf_s("%d %d", &n, &r);
int i;
for (i = 1; i <= n; i++)num.insert(i);
DFS(0,1);
return 0;
}
非递归方法没想出来,看到一个博主用了进位的思路,很有意思