[Luogu Brush Questions] Blue Bridge Cup Topic Breakthrough - Depth First Search - dfs (9)

Table of contents

Written in front:

Topic: P1025 [NOIP2001 Improvement Group] Number Division - Luogu | New Ecology of Computer Science Education (luogu.com.cn)

Title description:

Input format:

Output format:

Input sample:

Sample output:

Problem-solving ideas:

code:

AC !!!!!!!!!!

Write at the end:


Written in front:

How can I learn an algorithm well?

I personally think that systematic brushing of questions is particularly important,

Therefore, in order to learn depth-first search well, in order to use violent search to deal with the Blue Bridge Cup,

Without further ado, let's start quizzing right away!

Title: P1025 [NOIP2001 Improvement Group] Number Division - Luogu | New Ecology of Computer Science Education (luogu.com.cn)

Title description:

Input format:

n, k (6 < n ≤ 200,2 ≤ k ≤ 6)

Output format:

1 integer, that is, different divisions.

Input sample:

7 3

Sample output:

4

hint:

The four divisions are:
1, 1, 5;
1, 2, 4;
1, 3, 3;
2, 2, 3.

Problem-solving ideas:

When we use depth-first search,

The first point to note is the order of the search,

Because we want to make sure,

We have written a recursive structure capable of traversing all cases.

(It is always good to be more familiar with the basic idea of ​​recursive search above)

 Next is the specific idea :

We first draw a recursive search tree according to the topic conditions:

Root node: (take the example given in the title as an example)

Starting from the first position, we fill numbers starting from 1:

The question requires that the next number must be greater than or equal to the previous number,

 We found that the maximum requirement for the topic is 7, and the minimum sum starting from 3 is also 9,

We can directly judge that he is illegal and needs to be pruned.

Continue to search recursively:

 We found that when 1 + 4 + 4 > 7, 2 + 3 + 3 > 7, these two cases also need to be pruned afterwards,

To sum up the law of pruning, it is actually:

If the sum of the current values ​​+ the number of remaining positions * the initial value > the value required by the question,

There is no need to continue the recursive search.

Continue to search recursively:

 We can implement the code according to this rule: (remember to prune)

code:

//包含常用头文件
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int n, k;

//因为题目只需要返回计数,所以不用专门开一个数组记录,直接用一个变量计数即可
int res = 0;

void dfs(int u, int start, int sum)
{
    //遍历完三个位置
    if(u > k)
    {
        //如果符合条件
        if(sum == n)
        res++;
        return;
    }
    
    //如果当前的sum + 接下来剩下位置的数量 * 起始数值 > 题目要求的值,就不用继续递归搜索了
    for(int i = start; sum + i * (k - u + 1) <= n; i++)
    {
        dfs(u + 1, i, sum + i);
    }
}

int main()
{
    cin >> n >> k;
    dfs(1, 1, 0);
    printf("%d\n", res);
    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/129652109