The title describes the given array of nonnegative integers, assuming that your initial position is the first subscript of an array. Each element in the array represents the maximum length that you can jump in that position. Make sure you are able to jump to the last subscript of the array. For example: $A = [2,3,1,1,4]$ can jump to the last subscript, output true; $A = [3,2,1,0,4]$ cannot jump to the last subscript, output false. Enter the first line to enter a positive integer $n (1 \leq n \leq 500) $, the next line $n $ an integer, the input array $A _i$. Output true if you can skip to the last subscript, otherwise the output is false. Sample input
52 0 2) 0 1
Sample output
True
#include <iostream>#include<string>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<vector>#include<queue>#include<deque>#include<map>#defineRange (I,A,B) for (int i=a;i<=b;++i)#defineLL Long Long#defineRerange (I,A,B) for (int i=a;i>=b;--i)#defineFill (arr,tmp) memset (arr,tmp,sizeof (arr))using namespacestd;intn,num[505],dp[505];voidinit () {cin>>N; Range (I,0, N-1) cin>>num[i];}voidsolve () {BOOLans=false; if(n==1) ans=true; intpos=0; while(pos<n-1) {pos+=Num[pos]; if(!num[pos]&&pos!=n-1) Break; if(pos==n-1) {ans=true; Break;} } cout<< (ans?)"true":"false") <<Endl;}intMain () {init (); Solve (); return 0;}
View Code
JSK 18: Jumping game