[AcWing Brush Questions] Blue Bridge Cup Topic Breakthrough-Dynamic Programming-Introduction to dp (17)

Table of contents

Written in front:

Topic: 821. Jumping Stairs - AcWing Question Bank

Title description:

Input format:

Output format:

data range:

Input sample:

Sample output:

Problem-solving ideas:

Method 1: Brute force search

the code

Method 2: Memory search

the code

Method 3: Dynamic Programming

 the 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 dynamic programming well, deal with the "DP Cup".

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

Topic: 821. Jumping Stairs - AcWing Question Bank

Title description:

There are n steps in a staircase, and one or two steps can be walked at a time.

Ask how many ways there are to go from the 0th step to the nth step.

Input format:

A line containing an integer n.

Output format:

A total of one line, containing an integer, indicating the number of schemes.

data range:

1 ≤ n ≤ 15

Input sample:

5

Sample output:

8

Problem-solving ideas:

This question is a classic dp entry question,

I plan to use three methods to do it, advance layer by layer, and start recursion together:

Method 1: Brute force search

This question also appeared in the previous dfs brushing questions,

We can search all schemes with dfs:

Jumping three steps is the method of jumping two steps + jumping one step;

Jumping four steps is the method of jumping three steps + jumping two steps;

And so on: 

Finally got the answer:

code show as below:

the code

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int n = 0;

int dfs(int u)
{
    if(u == 1) return 1;
    else if(u == 2) return 2;
    
    return dfs(u - 1) + dfs(u - 2);
}

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

Method 2: Memory search

In fact, the time complexity of the first method is too high,

It is as big as 2 to the nth power:

If n >= 42, it will time out:

 

In order to reduce time consumption, we can use memoized search:

Record the calculated values ​​into an array,

In this way, the searched value does not need to be searched repeatedly:

 The following is the code implementation:

the code

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int n = 0;
int sum = 0;
const int N = 30;
int mem[N];//用一个数组记录

int dfs(int u)
{
    if(mem[u]) return mem[u];//如果记录过,就不用往下搜索了
    
    if(u == 1) return 1;
    else if(u == 2) return 2;
    else sum = dfs(u - 1) + dfs(u - 2);
    
    mem[u] = sum;//将该位置的值记录进数组
    return mem[u];
}

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

We found that after this, even if n = 100, it only takes 1ms:

Method 3: Dynamic Programming

We found that, in fact, the dynamic programming of this question

The state expression is derived from the brute force search,

Recursive search is to search from top to bottom,

Backtracking from bottom to top,

In fact, the process of getting the result is when backtracking:

Then we don't need to search for this process, and directly push it from bottom to top,

This is dynamic programming:

The following is the code implementation:

 the code

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int n = 0;
const int N = 30;
int fib[N];

int main()
{
    scanf("%d", &n);
    
    fib[1] = 1;
    fib[2] = 2;
    
    if(n == 1 || n == 2)
    {
        printf("%d", fib[n]);
        return 0;
    }
    
    for(int i = 3; i <= n; i++)
    {
        //这个就是动态规划的状态表达式
        fib[i] = fib[i - 1] + fib[i - 2];
    }
    printf("%d", fib[n]);

    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/129896223