Dynamic Programming-frog jumping

Source: Internet
Author: User

Description
There is a single-plank bridge on the river, and a frog wants to jump from one side of the river to the other along the plank. There were some stones on the bridge, and the frogs hated stepping on the stones. Since the length of the bridge and the distance that the frog skips all are positive integers, we can think of the point where the frog may arrive on the single plank as a series of the whole hour on the axis: 0,1,......,l (where L is the length of the bridge). A point with a coordinate of 0 represents the starting point of the bridge, and the point with the L coordinates represents the end point of the bridge. The frog jumps from the beginning of the bridge to the final direction. The distance of a single hop is any positive integer (including s,t) between S and T. when a frog jumps or jumps over a point with a coordinate of l, even if the frog has jumped out of the plank.

The topic gives the length of the bridge, the distance of the frog jumps s,t, and the position of the stone on it. Your task is to determine the minimum number of stones a frog will need to step across the river.
Input
There are multiple sets of test data.
For each set of test data, the first row of four positive integers L, s, T, N (1 <= L <= 10^5, 1 <= s <= t <= 10,1 <= n <= 100), respectively, represents the length of the bridge, the smallest distance the frog jumps at a time, the most Large distances, and the number of stones on the bridge. The second line has n different positive integers representing the position of the N stones on the axis (the data ensures that there are no stones at the beginning and end of the bridge). All adjacent integers are separated by a space.
Output
Each set of test data outputs only one row, including an integer that indicates the minimum number of stones a frog needs to step across the river.
Sample Input
10 2 3 5
2 3 5) 6 7
Sample Output

2

#include <stdio.h>#include<string.h>Const intmaxn=100020;intFLAG[MAXN];intDP[MAXN];intMain () {intL,s,t,n; intA;  while(SCANF ("%d%d%d%d", &l,&s,&t,&n)! =EOF) {memset (flag,0,sizeof(flag)); Memset (DP,-1,sizeof(DP));//initialization, 1 for unreachable//Dp[i] Indicates the minimum number of stones to be passed to point I, 1 means that it cannot be reached         for(intI=0; i<n;i++) {scanf ("%d",&a); Flag[a]=1;//There are stones for 1, otherwise 0} dp[0]=0;  for(inti=s;i<=l+t-1; i++)        {             for(intj=i-t;j<=i-s;j++)//J Point jumps to I point            {                if(j>=0&&dp[j]!=-1)//J Point can jump to                {                    if(dp[i]==-1) Dp[i]=dp[j]+flag[i];//give value directly for the first time                    Else if(Dp[i]>dp[j]+flag[i]) dp[i]=dp[j]+flag[i];//find a small value                                    }                }            }          intres=10000;  for(inti=l;i<=l+t-1; i++)//L to l+t-1 the smallest non-1 value        {            if(dp[i]!=-1&&dp[i]<res) res=Dp[i]; } printf ("%d\n", RES); }        return 0;} 

Dynamic Programming-frog jumping

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.