【AcWing】Blue Bridge Cup Preparation - Depth First Search - dfs (3)

Table of contents

Written in front:

Topic: 93. Recursive Implementation of Combined Enumeration - AcWing Question Bank

Reading question:

Input format:

Output format:

data range:

Input sample:

Sample output:

Problem-solving ideas:

code:

AC !!!!!!!!!!

Write at the end:


Written in front:

It has been less than a month since the Blue Bridge Cup,

According to rumors in the rivers and lakes,

The Blue Bridge Cup's favorite tests are depth-first search and dynamic programming.

Therefore, the Lanqiao Cup is also called the Baosou Cup and the DP Cup.

Then of course I started with depth-first search, the so-called dfs, to prepare for the game.

Topic: 93. Recursive Implementation of Combined Enumeration - AcWing Question Bank

Reading question:

Input format:

Two integers n, m, separated by spaces on the same line.

Output format:

Output all schemes in ascending order, 11 in each line.

First, the numbers in the same line are arranged in ascending order, and two adjacent numbers are separated by a space.

Secondly, for two different rows, compare the numbers corresponding to the subscripts one by one, and the one with the smaller lexicographical order is ranked first

(Example:  1 3 5 7 top 1 3 6 8 ).

data range:

n > 0,
0 ≤ m ≤ n,
n + (n − m) ≤ 25

Input sample:

5 3

Sample output:

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 

Problem-solving ideas:

This question is a classic question of depth-first search.

When we use depth-first search,

The first point to note is that we want to ensure that

The recursive structure we wrote can traverse all cases,

When we first learn to search, we must draw a recursive search tree observation,

Recursion is very abstract, and drawing pictures can help us solve problems very well. (It is always good to be more familiar with the basic idea of ​​recursive search above)

Next is the specific idea:

The requirement of the title is to choose m out of n numbers and output different options

And it should be arranged in ascending order, and the lexicographical order is small in front,

Let's first draw a recursive search tree to understand the idea:

The first is the root node: (take n=5, m=3 as an example)

 My idea is to fill in the data according to the location,

Because the title requires an ascending array, the first number can only be 1, 2, 3.

According to the requirements of the ascending order of the topics,

Then continue to search recursively:

 Continue to search recursively:

The last line is the value we need to print,

Next, implement the graphics into code: 

code:

//养成好习惯,把常用头文件包了
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

//根据题目要求决定数组大小
const int N = 30;

int n, m;

int st[N];

void dfs(int u, int start)
{
    //如果递归到底
    if(u == m)
    {
        //输出
        for(int i = 0; i < m; i++)
        {
            printf("%d ", st[i]);
        }
        puts("");
        return;
    }
    else
    {
        //不重复使用数字
        for(int i = start; i < n; i++)
        {
            st[u] = i + 1;
            dfs(u + 1, i + 1);
            st[u] = 0;
        }
    }
}


int main()
{
    scanf("%d %d", &n, &m);
    dfs(0, 0);
    return 0;
}

AC !!!!!!!!!!

Write at the end:

The above is the content of this article, thank you for reading.

If you like this article, please like and comment, and write down your opinions.

If you want to learn programming with me, you might as well follow me, we will learn and grow together.

I will output more high-quality content in the future, welcome to watch.

Guess you like

Origin blog.csdn.net/Locky136/article/details/129487614