Simple priority queue. I didn't learn it or thought it was difficult to do it (it seems that I didn't understand the original question !!!)
This morning, I took the lead !!!
Please join us in the morning! (I want to go home in two days. I have been training for more than a month. What should I do! If you are tired, you just don't want !!!)
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
(Nonsense: priority queue is easy to use! Priority !!! In this world, most of the problems are the most important, either the worst or the best, and the priority queue is undoubtedly good! Hey hey ·······)
# Include <stdio. h>
# Include <string. h>
# Include <queue>
Using namespace STD;
Int start, end, N;
Int A [500], visit [500];
Struct Node
{
Int X, time;
Friend bool operator <(node A, Node B)
{
Return A. Time> B. time;
}
};
Int DFS ()
{
Priority_queue <node> q;
Node cur, next;
Cur. x = start;
Cur. Time = 0;
Q. Push (cur );
Visit [start] = 1;
While (! Q. Empty ())
{
Next = Q. Top ();
Q. Pop ();
If (next. x = end)
Return next. time;
If (next. x + A [next. x] <= N & visit [next. x + A [next. x] = 0)
{
Cur. Time = next. Time + 1;
Cur. x = next. x + A [next. x];
Q. Push (cur );
Visit [next. x + A [next. x] = 1;
}
If (next. X-A [next. x]> = 1 & visit [next. X-A [next. x] = 0)
{
Cur. Time = next. Time + 1;
Cur. x = next. X-A [next. x];
Q. Push (cur );
Visit [next. X-A [next. x] = 1;
}
}
Return-1;
}
Int main ()
{
Int I, ans;
While (scanf ("% d", & N), n)
{
Memset (visit, 0, sizeof (visit ));
Scanf ("% d", & START, & End );
For (I = 1; I <= N; I ++)
Scanf ("% d", & A [I]);
Ans = DFS ();
Printf ("% d \ n", ANS );
}
Return 0;
}
Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1548