Hdu1548:a Strange Lift Dijkstra
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 Pres s 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 integers 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 53 3 1 2 50
Sample Output
3 Test Instructions: Given the number of floors N, the starting floor and the destination floor, ask at least several times up or down can reach the destination floor up, will rise to the i+k[i] layer, down will descend to the i-k[i] layer of the given data 1-up->4-up->6 super, back to 4-down- >2-up->5 has three steps.
1#include <stdio.h>2#include <iostream>3#include <string.h>4 #defineMax 0x3f3f3f3f5 6 using namespacestd;7 8 intMain ()9 {Ten intn,a,b,x; One inti,j,k,min; A intfloor[201][201],f[201],used[201]; - while(SCANF ("%d", &n) &&N) - { the -memset (Used,0,sizeof(used)); -scanf"%d%d",&a,&b); - + for(i=1; i<=n;i++) - { + for(j=1; j<=n;j++) Afloor[i][j]=Max; at -scanf"%d", &x);///The following is a record of the floors that can be reached on this floor, recorded as 1 - if(i+x<=N) -floor[i][i+x]=1; - if(i-x>=1) -floor[i][i-x]=1; in } - to for(i=1; i<=n;i++) +f[i]=Floor[a][i]; - thef[a]=0;//The initial value is the distance from the starting point to each point! * $ for(i=1; i<=n;i++)Panax Notoginseng { -min=Max; the for(j=1; j<=n;j++) + if(!used[j]&&f[j]<min) A { themin=f[j];k=J; + } - $ if(min==max) $ Break; -used[k]=1; - the for(j=1; j<=n;j++) - if(!used[j]&&f[j]>floor[k][j]+F[k])Wuyif[j]=floor[k][j]+F[k]; the } - Wu if(f[b]==max) -printf"-1\n"); About Else $printf"%d\n", F[b]); - } - return 0; -}
Hdu1548:a Strange Lift