A Strange Lifttime limit:2000/1000ms (java/other) memory limit:65536/32768k (Java/Other) Total Submis Sion (s): 64 accepted submission (s): 29font:times New Roman | Verdana | Georgiafont Size:←→problem Descriptionthere 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 '? Inputthe input consists of several test Cases.,each test case contains both 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. Outputfor each case of the input output a interger, the least times you had to press the button when you were on floor A,and y ou want 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
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<climits>using namespacestd;intA,b,ans,i,n;intf[205];intstep[205];voidDfsintFloorintk) { if(floor==b) {if(ans==-1) ans=K; Elseans=min (ans,k); return; } if(Floor>n | | floor<1)return; if(K>=step[floor])return; Step[floor]=K; DFS ( floor+f[floor],k+1); DFS ( floor-f[floor],k+1); return;}intMain () { while(SCANF ("%d",&N) {scanf ("%d%d",&a,&b); for(i=1; i<=n;i++) {scanf ("%d",&F[i]); Step[i]=Int_max; } ans=-1; DFS (A,0); printf ("%d\n", ans); } return 0;}/*BFS: Second method # include <iostream> #include <cstdio> #include <algorithm> #include <cstring># Include<queue>using namespace Std;int a,b,ans,i,n;int f[205];int vis[205];struct node{int floor,step;}; int check (int a) {if (a<1 | | a>n | | vis[a]) return 0; return 1;} int main () {while (scanf ("%d", &n), N) {scanf ("%d%d", &a,&b); for (i=1;i<=n;i++) scanf ("%d", &f[i]); if (a==b) {printf ("0\n"); continue;} memset (vis,0,sizeof (VIS)); queue<node>s; Node p; P.floor=a; P.step=0; S.push (P); Vis[a]=1; Ans=-1; while (!s.empty ()) {P=s.front (); S.pop (); int X=p.floor+f[p.floor]; if (check (x)) {vis[x]=1; Node q; Q.floor=x; q.step=p.step+1; S.push (q); if (x==b) {ans=q.step;break;} } X=p.floor-f[p.floor]; if (check (x)) {vis[x]=1; Node q; Q.floor=x; q.step=p.step+1; S.push (q); if (x==b) {ans=q.step;break;} }} printf ("%d\n", ans); } return 0;}*/
Hdu1548:a Strange Lift