A Strange Lift Time
limit:1000MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64 U Submit Status Practice HDU 1548
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 53 3 1 2 50
Sample Output
3
BFS Topic Direct template can be one of them is
if "down" to negative floor, then the number of layers, rather than down to the 1 floor;(I think no one can read it wrong ...) )
1 /*2 By:ohyee3 Github:ohyee4 Email:[email protected]5 Blog:http://www.cnblogs.com/ohyee/6 7 かしこいかわいい? 8 エリーチカ! 9 to write out the хорошо code OH ~Ten */ One A#include <cstdio> -#include <algorithm> -#include <cstring> the#include <cmath> -#include <string> -#include <iostream> -#include <vector> +#include <list> -#include <queue> +#include <stack> A using namespacestd; at - //DEBUG MODE - #defineDebug 0 - - //Loops - #defineREP (n) for (int o=0;o<n;o++) in - Const intMAXN =205; to intK[MAXN]; + intN; - the intBFS (intSintv) { * if(s = =v) $ return 0;Panax Notoginseng -queue<int>Q; the BOOLVISITED[MAXN]; +Memset (visited,false,sizeof(visited)); A intDIS[MAXN]; thememset (DIS,0,sizeof(DIS)); + - Q.push (s); $Visited[s] =true; $ while(!Q.empty ()) { - intth =Q.front (); - Q.pop (); the - //reach the endWuyi if(th = =v) the Break; - Wu //Expand Nodes - intNext; About for(inti =-1; i = =-1|| i = =1; i + =2) { $Next = th + I *k[th]; - if(Next > N | | next <=0) - Continue; - if(!Visited[next]) { A Q.push (next); +Visited[next] =true; theDis[next] = dis[th] +1; - } $ } the the } the the if(Dis[v]) - returnDis[v]; in Else the return-1; the } About the BOOLDo () { the ints,v; the if(SCANF ("%d%d%d", &n,&s,&v), N = =0) + return false; - REP (N) thescanf"%d", &k[o +1]);Bayiprintf"%d\n", BFS (s,v)); the return true; the } - - intMain () { the while(Do ()); the return 0; the}
HDU 1548.A Strange Lift