Kangaroo crossing the river (Dynamic Planning)

Source: Internet
Author: User

Description
A kangaroo will jump from the river side to the other side of the river. The river is very wide, but there are many piles in the middle of the river. There is one every other meter, and each pile has a spring, kangaroo jump to the spring to jump farther. Each spring has different power. A number represents its power. If the spring power is 5, the kangaroo can jump up to 5 meters next hop. If the spring power is 0, it will be stuck and cannot continue to jump. The river is n meters wide in total, and the kangaroo is first located on the first spring. If it is to jump to the last spring, it will even cross the river. Given the strength of each spring, the minimum number of hops required by Kangaroo to reach the other side. If you cannot reach output-1
Input description:

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

Output description:

The minimum number of output hops cannot reach output-1.

Example 1
Input

5
2 0 1 1 1

Output

4

N = int (input () Tan = List (MAP (INT, input (). split () dp = [100000 for I in range (n + 1)] DP [0] = 0for I in range (n): For J in range (1, tan [I] + 1): If I + j <n + 1: DP [I + J] = min (DP [I + J], DP [I] + 1) print (DP [-1] If DP [-1]! = 100000 else-1)

Kangaroo crossing the river (Dynamic Planning)

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.