JSK 18: Jumping game

Source: Internet
Author: User

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

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.