HDU 1548 A Strange Lift

Source: Internet
Author: User

Topic Connection

http://acm.hdu.edu.cn/showproblem.php?pid=1548

A Strange Liftdescription

There is a strange lift. The lift can stop can at every floor as you want, and there are a number $K _i (0 \leq k_i \leq N) $ on every floor. The lift has just, buttons:up, and down. When you are at floor i,if Press the button "up", you'll go up Ki floor,i.e,you'll go to the i+ki th floor,as the SAM E, if you press the button ' down ', you'll go down Ki floor,i.e,you'll go to the $i-k_i$ th floor. Of course, the lift can ' t go up high than $N $,and can ' t go down lower than 1. For example, there are a buliding with 5 floors, and $k _1 = 3, k_2 = 3,k_3 = 1,k_4 = 2, k_5 = 5$. Begining from the 1st Floor,you can press the button "up", and you'll go up to the 4th Floor,and if you press the button "Down", the lift can ' t do it, because it can ' t go down to the-2nd floor,as you know, the-2nd floor isn ' t exist.
Here comes the problem:when is on floor a,and you want to go to floor b,how many times at least he have to press the Button ' up ' or ' down '?

Input

The input consists of several test Cases.,each test case contains the lines.
The first line contains three integers $N, \ a,\ B (1 \leq n,a,b \leq) $ which describe above,the second line consist $N $ integers $k _1,k_2,.... k_n$.
A single 0 Indicate the end of the input.

Output

For each case of the input output a interger, the least times you had to press the button when you were on the floor a,and you wan T to go to floor b.if your can ' t reach floor b,printf "-1".

Sample Input

5 5 5
3 3 1) 2 5
0

Sample Output

3

Simple BFS ...
1#include <algorithm>2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <vector>7#include <queue>8#include <map>9 usingstd::cin;Ten usingstd::cout; One usingStd::endl; A usingStd::find; - usingStd::sort; - usingStd::map; the usingstd::p air; - usingstd::vector; - usingStd::queue; - #definePB (E) push_back (e) + #defineSZ (c) (int) (c). Size () - #defineMP (A, b) Make_pair (A, B) + #defineAll (c) (c). Begin (), (c). End () A #defineITER (c) Decltype ((c). Begin ()) at #defineCLS (arr,val) memset (arr,val,sizeof (arr)) - #defineCpresent (c, E) (Find (All (c), (e))! = (c). End ()) - #defineRep (i, n) for (int i = 0; i < (int) (n); i++) - #defineTR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) - Const intMax_n = About; - Const intDx[] = {-1,1 }; intypedef unsignedLong Longull; - BOOLVis[max_n]; to intN, A, B, arr[max_n]; + structNode { -     intx, S; theNode (inti =0,intj =0): X (i), S (j) {} * }; $ voidBFs () {Panax NotoginsengCLS (Vis,0); -Queue<node>que; theQue.push (Node (A),0)); +Vis[a] =true; A      while(!Que.empty ()) { theNode TP =Que.front (); Que.pop (); +         if(tp.x = = B) {printf ("%d\n", TP.S);return; } -Rep (I,2) { $             intNX = dx[i] * arr[tp.x] +tp.x; $             if(NX <0|| NX > N | | VIS[NX])Continue; -Que.push (The Node (NX, TP.S +1)); -VIS[NX] =true; the         } -     }WuyiPuts"-1"); the } - intMain () { Wu #ifdef LOCAL -Freopen ("In.txt","R", stdin); AboutFreopen ("OUT.txt","w+", stdout); $ #endif -      while(~SCANF ("%d", &n) &&N) { -scanf"%d%d", &a, &B); -Rep (i, N) scanf ("%d", &arr[i +1]); A BFS (); +     } the     return 0; -}
View Code

HDU 1548 A Strange Lift

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.