Time limit: 1000ms Memory limit: 65536K
Given an array of non-negative integers, assume your initial position is the first index of the array.
Each element in the array represents the maximum length you can jump at that position.
Your goal is to reach the last subscript and use the fewest number of jumps.
E.g:
A = [2,3,1,1,4] , the minimum number of jumps to reach the last subscript is 2 . (First jump 1 step, from subscript 0 to 1 , then jump 3 steps to the last subscript. Twice in total)
input format
Enter a positive integer n ( 1 ≤ n ≤ 1 0 0 ) in the first line, and enter n integers in the next line, representing the array A .
output format
Finally output the minimum number of jumps.
sample input
5 3 1 1 1 1
Sample output
2
Code:
#include <iostream> #include <cstring> using namespace std; int board[105];//Record the input data int Re[105];//Record the minimum number of steps to reach each grid int main(){ int N; cin>>N; for(int i=0 ; i<N ; i++){ cin>>board[i]; } memset(Re,0x3f3f3f3f,sizeof(Re)); Re[0] = 0;//The minimum number of steps to reach the initial grid is 0 for(int i=0 ; i<N ; i++){ for(int j=i ; j<=i+board[i] ; j++){ if(Re[j] > Re[i]+1){ Re[j] = Re[i]+1; } } } cout<<Re[N-1]; return 0; }