A Strange Lift
Description
There is a strange lift. The lift can stop can at every floor as you want, and there are a number ki (0 <= ki <= 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-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 are a buliding with 5 floors, and K1 = 3, K2 = 3,K3 = 1,k4 = 2, K5 = 5.Begining from the 1st Floor,you Can press the button "up", and you'll go up to the 4th Floor,and if your press the button "down", the lift can ' t does it, be Cause 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 <= n,a,b <=) which describe above,the second line consist N int Egers k1,k2,.... kn.
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 1 5
3 3 1) 2 5
0
Sample Output
3
Main topic:
Once there was a building, there is an elevator inside the building, the elevator is very wonderful.
A building has N-level (1-n), floor I elevator can go to i-k[i] and i+k[i] layer, (k array topic given) (that is, press the button)
The minimum number of press buttons from layer A to layer B.
The idea of knot problem:
BFS is solvable and has done a similar problem, so use the Dijskstra algorithm here.
The shortest path Dijskstra algorithm can be solved, begin is i,end to I+k[i]&&i-k[i], and the weighted value of the edge is 1. Finding the shortest path from A to B is the answer. (Note as a graph of direction )
Code:
1#include <stdio.h>2#include <limits.h>3#include <iostream>4#include <string.h>5 #defineMAXN 2006 using namespacestd;7 intedge[maxn+Ten][maxn+Ten];8 intdis[maxn+Ten];9 BOOLvis[maxn+Ten];Ten intt,s,d,n,k; One voidDijskstra (intbegin) A { -memset (Vis,0,sizeof(Vis)); - for(intI=1; i<=t; i++) thedis[i]=Int_max; -dis[begin]=0; - for(intt=1; t<=t; t++) - { +vis[begin]=1; - for(intI=1; i<=t; i++) + if(!vis[i]&&edge[begin][i]!=int_max&&dis[begin]+edge[begin][i]<Dis[i]) Adis[i]=dis[begin]+Edge[begin][i]; at intmin=Int_max; - for(intj=1; j<=t; J + +) - if(!vis[j]&&min>Dis[j]) - { -min=Dis[j]; -begin=J; in } - } to } + intMain () - { the intBegin,end; * while(cin>>T) $ {Panax Notoginseng if(t==0) Break; - for(intI=1; i<=maxn;i++) the for(intj=1; j<=maxn;j++) +edge[i][j]=Int_max; Ascanf"%d%d",&begin,&end); the intT; + for(intI=1; i<=t;i++) - { $scanf"%d", &t);//Note that there is a map! At first because of this WA several times. $ if(i+t<=t) edge[i][i+t]=1; - if(i-t>=1) edge[i][i-t]=1; - } the Dijskstra (begin); - if(Dis[end]!=int_max) printf ("%d\n", Dis[end]);Wuyi Elseprintf"-1\n"); the } - return 0; Wu}