A strange lift
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 7965 accepted submission (s): 3002
Problem descriptionthere is a strange lift. the lift 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 you at floor I, if you press the button "up", you will
Go up Ki floor, 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 th floor. of course, the lift can't go up high than N, and can't go down lower than 1. 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 the-2
Th floor, 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 floor B, how many times at least he has to press the button "up" or "down "?
Inputthe input consists of several test cases., each test case contains two lines.
The first line contains three integers n, a, B (1 <= n, a, B <= 200) which describe above, the second line consist n integers K1, k2 ,.... kN.
A single 0 indicate the end of the input.
Outputfor each case of the input output a interger, the least times you have to press the button when you on floor a, and you want to go to floor B. if you can't reach floor B, printf "-1 ".
Sample Input
5 1 53 3 1 2 50
Sample output
3
Recommend8600
Question. There is a strange elevator. A digital Ki is given for each floor by the question. You can only go to or from the ki building. Of course, it cannot exceed the floor limit. It is impossible to get to the ground from the last day.
How many buttons are pressed from layer a to layer B at least?
# Include <stdio. h> # include <string. h> int maps [250] [250], DIS [250], OK [250]; // int N for the shortest path of a single source under a vertex; int INF = 100000000; void Dijk () {int I, T, P, mi; t = n; while (t --) {mi = inf; for (I = 1; I <= N; I ++) {If (mi> dis [I] &! OK [I]) {mi = dis [I]; P = I ;}} OK [p] = 1; for (I = 1; I <= N; I ++) {If (! OK [I] & maps [p] [I] + dis [p] <dis [I]) dis [I] = maps [p] [I] + dis [p] ;}} int main () {int I, j, A, B, D, up, down; while (scanf ("% d", & N), n) {memset (OK, 0, sizeof OK); scanf ("% d", &, & B); for (I = 1; I <= N; I ++) // necessary initialization {dis [I] = inf; For (j = 1; j <= N; j ++) {if (I = J) maps [I] [J] = 0; maps [I] [J] = inf ;}} for (I = 1; I <= N; I ++) {scanf ("% d", & D); up = I + D; // record whether two floors are connected down = I-d; If (up <= N) maps [I] [Up] = 1; // if it is the same, click it. If (down> = 1) maps [I] [d Own] = 1;} dis [a] = 0; Dijk (); If (DIS [B]! = Inf) printf ("% d \ n", DIS [B]); else printf ("-1 \ n");} return 0 ;}