Kangaroo crossing the river

Kangaroo crossing the river

Time limit: 1 second Space limit: 32768K

Topic description

A kangaroo wants to jump from one side of the river to the other side of the river. The river is very wide, but there are many stakes in the middle of the river. There is one every one meter. There is a spring on each stake. The kangaroo can jump on the spring. farther. The strength of each spring is different, and a number is used to represent its strength. If the spring strength is 5, it means that the kangaroo can jump up to 5 meters in the next jump. If it is 0, it will get stuck and cannot continue to jump. The river is N meters wide. The initial position of the kangaroo is on the first spring. After jumping to the last spring, the river is crossed. Given the force of each spring, find out how many jumps the kangaroo needs at least to reach the opposite bank. If the output cannot be reached -1

Enter description:

The input is divided into two lines, the first line is the array length N (1 ≤ N ≤ 10000), and the second line is the value of each item, separated by spaces.

Output description:

Output the minimum number of hops, unable to reach output -1

Example 1

enter

5
2 0 1 1 1

output

4
#include <stdio.h>
#include <string.h>
#define min(a, b) a < b ? a : b
int a[10010], dp[10010];
intmain()
{
    int n;
    while (~scanf("%d", &n))
    {
        dp[0] = 0;
        for (int i = 1; i < n+1; i++)
            dp[i] = 999999;
        for (int i = 0; i < n; i++)
            scanf("%d", a + i);
        for (int i = 1; i <= n; i++)
        {
            for (int j = 0; j < i; j++)
            {
                if (a[j] + j >= i)
                    dp[i] = min(dp[j] + 1, dp[i]);
            }
        }
        if (dp[n] == 999999)
            printf("-1\n");
        else printf("%d\n", dp[n]);
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324836307&siteId=291194637