Kangaroo Crossing the River (Dynamic Programming)

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, one every one meter, and there is a spring on each stake, and the kangaroo can jump by jumping on the spring. farther. The strength of each spring is different, and a number represents 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
1  import java.util.ArrayList;
 2  import java.util.List;
 3  import java.util.Scanner;
 4  /** 
5  * 
 6  * Kangaroo crossing the river (up to the number of steps to jump the spring) Use list to simulate dp array list Save each line, the abscissa is the arrival of each spring, the arrival is 0, the arrival is 1, and the arrival here is 0
 7  * 
 8  * @author Dell
 9  *
 10   */ 
11  public  class Main {
 12      static List< int []> dp = new ArrayList();
 13      static  int n;
 14      static  int[] is;
 15  
16      static  int f() {
 17          // 18 while standing on the first spring 
int step = 0 ;
 19 int x = 0 ;
 20 int [] s = new int [n + 1 ];
 21 for ( int j = 0; j < s.length; j++ ) {
 22              s[j] = 0 ;
 23         }
 24          s[0] = 1 ;
 25         dp.add(s);
 26 while (step <= n) { // all lines 27 int                                                
             [] ss = new  int [n + 1]; // The last extra bit represents shore 
28              for ( int j = 0; j < ss.length; j++ ) {
 29                  ss[j] = 0 ;
 30              }
 31              // For each line, fill in the next line 
32 according to this line              for ( int i = step; i < ss.length; i++) { // The last extra digit represents the shore
 33                  // The position i has reached 
34                  if (dp.get (dp.size() - 1)[i] == 1 ) {
 35                      // take different steps 
36                      for ( int j = 1; j <= is[i]; j++ ) {
37                          // Judging not to exceed the spring range one by one, the subscript should be reduced more, if it exceeds, select the shore as 1 
38                          if (i + j <= ss.length-2 ) { 
 39                              // The new position in the range is not 0 no Trap access 
40                              if (is[i + j] > 0 ) {
 41                                  ss[i + j] = 1 ;
 42                              }
 43                          } else {
 44                              // If it exceeds, select the last one as 1 
45                              ss[ss.length - 1 ] = 1 ;
 46                          }
 47                  }
 48                  }
 49             }
50             dp.add(ss);
51 
52             step++;
53             if (dp.get(dp.size()-1)[ss.length - 1] == 1) {// 岸上访问到
54                 return step;
55             }
56         }
57         return -1;
58     }
59     public static void main(String[] args) {
60         Scanner sc = new Scanner(System.in);
61         n = sc.nextInt();
62         is = new int[n];
63         for (int i = 0; i < is.length; i++) {
64             is[i] = sc.nextInt();
65         }
66         int res = f();
67         System.out.println(res);
68     }
69 }

 

Guess you like

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