Hdus 1548 a strange lift

Source: Internet
Author: User

Problem description

There is a strange lift. thelift can stop can at every floor as you want, and there is a number KI (0 <= Ki <= N) on every floor. the lift have just two buttons: Up and down. when youat floor I, if you press the button
"Up", you will go up kifloor, I. e, you will go to the I + Ki th Floor, as the same, if you press the button "down", you will go down Ki floor, I. e, you will go to the I-ki thfloor. of course, the lift can't go up high than N, and can't go down lower than1. for example,
There is a buliding with 5 floors, and k1 = 3, K2 = 3, K3 = 1, K4 = 2, K5 = 5. begining from the 1 st floor, you can press the button "up", and you'll go up to the 4 th floor, and if you press the button "down ", the lift can't do it, because it can't go down to
-2 thfloor, as you know, the-2 th Floor isn' t exist.
Here comes the problem: when you are on floor a, and you want to go to floorb, how many times at least he has to press the button "up" or "down "?

 

Input

The input consists of severaltest cases., each test case contains two lines.
The first line contains three integers n, a, B (1 <= n, a, B <= 200) whichdescribe above, the second line consist n integers K1, k2 ,.... kN.
A single 0 indicate the end of the input.

 

Output

For each case of the InputOutput A interger, the least times you have to press the button when you onfloor A, and you want to go to floor B. if you can't reach floor B, printf "-1 ".

 

Sample Input

5 1 5

3 3 1 2 5

0

 

Sample output

3

 

 

Introduction: A strange elevator. The I-th floor can only be attached to the ki layer or lower Ki layer. I + Ki cannot exceed the top floor or I-ki cannot reach the negative building. How many times does it take to start from layer a to layer B? If it cannot reach output-1.

Method: the shortest path. Dijkstra. We can regard the reachable floors as a connection and the distance is 1.

 

 

# Include <stdio. h> # include <stdlib. h> # include <string. h> # define INF limit 00int n, a, B, K [210], DIS [210] [210], p [210]; // define stravoid Dijk () {int I, j, X, Y, Max, Count = 0; memset (K, 0, sizeof (k); // initialize for (I = 1; I <= N; I ++) {P [I] = dis [a] [I];} p [a] = 1; for (I = 1; I <n; I ++) {max = inf; For (j = 1; j <= N; j ++) {If (! K [J] & P [J] <max) {max = P [J]; X = J ;}} if (max = inf) break; K [x] = 1; if (K [B]) break; For (j = 1; j <= N; j ++) {If (! K [J] & P [J]> P [x] + dis [x] [J]) {P [J] = P [x] + dis [x] [J] ;}} if (! K [B] | P [B] = inf) {printf ("-1 \ n");} else {printf ("% d \ n ", P [B]) ;}}; int main () {int M, I, j, C; while (scanf ("% d", & N )! = EOF, n) // number of floors {scanf ("% d", & A, & B); // start point and end point for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) {dis [I] [J] = inf ;}} // The initialization distance is for (I = 1; I <= N; I ++) {scanf ("% d", & C ); // number of floors that can be upgraded or downgraded on each floor if (C = 0) continue; if (I + C <= N) {dis [I] [I + C] = 1; // The weight is 1} if (I-c> 0) {dis [I] [I-c] = 1; // The weight is 1 }}if (A = B) {printf ("0 \ n"); continue ;} if (A <1 | B <1 | A> N | B> N) {printf ("-1 \ n"); continue;} Dijk ();} system ("pause"); Return 0 ;}

 

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.