当前位置:网站首页>Introduction to depth first search (DFS)
Introduction to depth first search (DFS)
2022-07-27 08:14:00 【Bamboo monk-】
Depth-first search (Deep first serch, abbreviation dfs) yes A search method based on recursion .
You can tell by the name , It's pressing Depth direction The search of . in other words , Choose a way directly to the end , Then choose another way .
Look at the picture

The sequence of program execution is shown in the figure , Go to the end first , Choose another way .
Example :
The original title is
http://ybt.ssoier.cn:8088/problem_show.php?pid=1317【 Title Description 】
Permutation and combination are common mathematical methods , The combination is from n Out of these elements r Elements ( No order and r≤n), We can simply put n Elements are understood as natural numbers 1,2,…,n, Take any of them r Number .
Now you are required to output all combinations recursively .
for example n=5,r=3, All combinations are :
1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5
【 Input 】
Line two natural numbers n、r(1<n<21,1≤r≤n).
【 Output 】
All combinations , Each combination takes up a row, and the elements are arranged from small to large , Each element occupies a position of three characters , All the combinations are also in dictionary order .
【 sample input 】
5 3
【 sample output 】
123
124
125
134
135
145
234
235
245
345
Examine the subject : The first number of this problem is from 1 Start , All the way up to 3( Because it requires dictionary order ), The remaining numbers can be enumerated in turn
practice :
1. Defining variables 、dfs function
int n,r,res[30];
void dfs(int num,int pre)// Declare functions
{
if(num>r)// If the number of enumerations exceeds r, Output
{
for(int i=1;i<=r;i++) printf("%3d",res[i]);// Each element occupies three characters
cout<<endl;
return;
}
for(int i=pre+1;i<=n;i++)
{
res[num]=i;
dfs(num+1,i);// recursive
}
}
2. Input , The ginseng
int main()
{
cin>>n>>r;
dfs(1,0);// first floor
return 0;
}Complete code :
#include<bits/stdc++.h>
using namespace std;
int n,r,res[30];
void dfs(int num,int pre)// Declare functions
{
if(num>r)// If the number of enumerations exceeds r, Output
{
for(int i=1;i<=r;i++) printf("%3d",res[i]);// Each element occupies three characters
cout<<endl;
return;
}
for(int i=pre+1;i<=n;i++)
{
res[num]=i;
dfs(num+1,i);// recursive
}
}
int main()
{
cin>>n>>r;
dfs(1,0);// first floor
return 0;
}Novice , Please give me more guidance
边栏推荐
- I can't figure out why MySQL uses b+ trees for indexing?
- How to obtain the cash flow data of advertising services to help analyze the advertising effect?
- Qt Creator代码风格插件Beautifier
- SSTI template injection
- [applet] how to get wechat applet code upload key?
- Lu Xun: I don't remember saying it, or you can check it yourself!
- Shenzhi Kalan Temple
- 擎创科技加入龙蜥社区,共建智能运维平台新生态
- 情人节,我用字符画出了一个对象!
- Debug: generic related "unresolved external symbols"
猜你喜欢

Sword finger offer 58 - I. flip word order
![[geek challenge 2019] finalsql 1](/img/a7/857d47639fcb38e0055a2444206b8c.png)
[geek challenge 2019] finalsql 1

idea远程调试

Five day travels to Beijing

I can't figure out why MySQL uses b+ trees for indexing?

Graph node deployment and testing

1024 | in the fourth year officially called Menon, the original intention is still there, and continue to move forward

ERP生产作业控制 华夏

What is the real HTAP? (1) Background article

擎创科技加入龙蜥社区,共建智能运维平台新生态
随机推荐
The code interface is a little automated
Qt Creator代码风格插件Beautifier
Methods of server network testing
C commissioned use cases
C language: optimized Hill sort
mqtt指令收发请求订阅
Shell Scripts相关
How to obtain the cash flow data of advertising services to help analyze the advertising effect?
服务器网络测试的方法
一段平平无奇的秋招经历
File name wildcard rules for kettle
Database startup report error_ user_ connect_ times &gt; 0 error
[applet] the upload of the wechat applet issued by uniapp failed error: error: {'errcode': -10008,'errmsg':'Invalid IP
C language: random number + Hill sort
2020国际机器翻译大赛:火山翻译力夺五项冠军
JS access cookie example
The seta 2020 international academic conference will be held soon. Welcome to attend!
IBM3650M4实体机安装VCenter7.0
CommonTitleBar hide left right
Solid smart contract development - 3.3-solid syntax control structure