LeetCode jumping game (dynamic programming algorithm title)

I undertake on a blog content, with another algorithm pseudo-code to record the
chiefs of blog
Here Insert Picture Description
content for dynamic algorithm, in fact, it is a bottom-up process, that is, to push back and forth from the known position of the content like this Like the title, we know the focus, so we can begin to push back that step before the end of a point. The idea is you first need to mark the end of this position is correct, and then start a for loop from the front end of a location, determine the number of steps in that position to the end, can reach.

#include <stdio.h>
#include <stdlib.h>
//以下是伪代码
//首先是一个记录每一步的步数和位置的数组,数组下标是位置,数组里内容是步数
int a[100];
//其次便是一个dp的数组,来判断这个位置可不可以到达终点
int dp[100];
//然后就开始写方法
bool  jum_can(a){
    dp[len(a)-1]=1;//先标记终点这个位置是可以到终点的(也就是已知解)
    for(int i=len(a)-2;i>=0;i--){//从后往前推
        //先判断能走的最长的步数
        int long_step=min(len(a)-1,a[i]+i);//也就是数组长度减一和当前位置加上当前位置的步数
        //进行步数内的第二次循环,判断能不能到达
        for(int j=0;j<=long_step;j++){
            if(dp[j+a[i]==1)//说明当前位置加上步数是能到达的
            {
                dp[i]=1;//如果那个步数能达到,就说明这个位置能达到
            }
        }
    }
    if(dp[0]==1){//就是第一个位置能不能达到
        return true;
    }
    else
        return false;
}

//最后在主函数处
int main()
{
    //创建数组并输入数组数据
    int a[100];
    //(跳过输入部分)
    if(jum_can(a))//从0开始
        printf("可以到达");
    else
        printf("不可以到达")return 0;
}

Published 72 original articles · won praise 5 · Views 2831

Guess you like

Origin blog.csdn.net/qq_41115379/article/details/104838037