Dynamic Programming Topic (II.)--Jumping steps

Source: Internet
Author: User

Dynamic Programming Topic (II.)--Jumping steps

1. Title Description


A step has a total of n levels, if you can jump 1 levels at a time, you can jump 2 levels.

How many total hops are in total, and the time complexity of the algorithm is analyzed.


2. Recursive method


For this dynamic planning problem, we have two steps to think of:


    • If we jump 1 levels, then the rest of the jump is f (n-1);
    • If we jump 2 levels, then the rest of the jump is f (n-2);

this time we will be able to implement the recursive, slow! We also need to jump out of the recursive conditions, this is also essential!
For This topic, the condition of jumping out of recursion is when n==1, 2, we return 1, 2.
Why is it? Because when N=1, there is only one kind of jumping method, when n=2, there are two kinds of jumping method. Then there are the initial values, and there are only two cases (either jumping 1 levels or jumping 2 levels), so This is a Fibonacci sequence problem!


The code is as follows:


#include <iostream>using namespace std; int findsolution (int N) {int result[3]={0, 1, 2};   Initial condition if (n<=2) return result[n]; Return Findsolution (N-1) +findsolution (N-2);   Recursive implementation}int Main () {int n=20; int num=findsolution (N); Cout<<num<<endl;return 0;}




3. Non-recursive mode



The non-recursive way is also must be mastered!


If this is a Fibonacci sequence problem, then the non-recursive way to achieve it is relatively easy ~


As follows:

#include <iostream>using namespace std; int findsolution (int N) {int fab[3]={1,1}, if (n<2) return 1; for (int i=2; i<=n; i++) {fab[2]=fab[0]+fab[1];   It is necessary to pay attention to the calculation rule fab[0]=fab[1]; FAB[1]=FAB[2]; }return fab[2]; }int Main () {int n=20; int num=findsolution (N); Cout<<num<<endl;return 0;}



It should be pointed out that the problem of the non-recursive method of the calculation of the rule is better, then the "Dynamic Planning (A)" in the article is not very good to think, we must pay attention to the different problems of the calculation rules!


In addition, if you can jump 3 levels, then the implementation of the idea is the same, just need to replace the initial number of values can be ~



Dynamic Programming Topic (II.)--Jumping steps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.