A B C D E F G c-a Strange lift crawling in process ... crawling failed time Limit: 1000ms Memory Limit: 32768kb 64bit IO Format: %i64d &%i64u Submit Status Practice hdu 1548 appoint Description:syste M Crawler (2016-05-23)
Description There is a strange lift. The lift can stop can at every floor as for you want, and there are a number ki (0 <= ki <= N) on every floor. The lift have just two buttons:up and down. When you are in floor i,if you 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 to 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 your are on floor a,and your want to go to floor b,how The times in many he least to press The button ' up ' or ' down '?
Input the input consists of several test Cases.,each test case contains two lines.
The I 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" input.
Output for each case of the input output a interger, the least times your have to press the button while you are on floor A,and You are want to the floor b.if you can ' t reach floor b,printf "-1".
Sample Input
5 1 5 3 3 1 2 5 0
Sample Output
3 Wide Search ~
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
int st,ed,n;
int a[205];
BOOL vis[205];
struct node
{
int pos;
int cost;
friend bool operator< (node X,node y)
{return
x.cost>y.cost;
}
};
int BFS ()
{
priority_queue<node>s;
while (!s.empty ()) S.pop ();
Node TEMP,TEMP1;
temp.pos=st,temp.cost=0;
S.push (temp);
while (!s.empty ())
{
temp=temp1=s.top (); S.pop ();
if (temp.pos==ed) return temp.cost;
if (Vis[temp.pos]) continue;
Vis[temp.pos]=true;
Temp.pos+=a[temp.pos];
temp.cost++;
if (temp.pos<=n)
s.push (temp);
TEMP=TEMP1;
Temp.pos-=a[temp.pos];
temp.cost++;
if (temp.pos>=1)
s.push (temp);
}
return-1;
}
int main ()
{
while (~scanf ("%d", &n) &&n)
{
memset (vis,0,sizeof (Vis));
scanf ("%d%d", &st,&ed);
for (int i=1;i<=n;i++)
scanf ("%d", &a[i]);
printf ("%d\n", BFS ());
}
return 0;
}