Hangdian 1548 a strange lift (Guang suo)

Source: Internet
Author: User
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1548 A strange lift Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 11341 accepted submission (s): 4289


Problem descriptionthere is a strange lift. the lift can stop can at every floor as you want, and there is a number KI (0 <= Ki <= N) on every floor. the lift have just two buttons: Up and down. when you at floor I, if you press the button "up", you will go up Ki floor, I. e, you will go to the I + Ki th Floor, as the same, if you press the button "down", you will go down Ki floor, I. e, you will 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 is a buliding with 5 floors, and k1 = 3, K2 = 3, K3 = 1, K4 = 2, K5 = 5. begining from the 1 st floor, you can press the button "up", and you'll go up to the 4 th floor, and if you press the button "down ", the lift can't do it, because it can't go down to the-2 th Floor, as you know, the-2 th Floor isn' t exist.
Here comes the problem: when you are on floor a, and you want to go to floor B, how many times at least he has to press the button "up" or "down "?
 
Inputthe input consists of several test cases., each test case contains two lines.
The first line contains three integers n, a, B (1 <= n, a, B <= 200) which describe above, the second line consist n integers 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 have to press the button when you on floor a, and you want to go to floor B. if you can't reach floor B, printf "-1 ".
Sample Input
5 1 53 3 1 2 50
 
Sample output
3
AC code:
<Span style = "font-size: 24px; "> # include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <queue> using namespace STD; const int maxn = 201; int n, a, B; int Kp [maxn]; bool mark [maxn]; struct node {int floor; int tme ;}; int BFS () {memset (mark, false, sizeof (Mark); queue <node> q; node first, next_up, next_down; first. TME = 0; first. floor = A; q. push (first); Mark [a] = T Rue; // mark while (! Q. empty () {First = Q. front (); q. pop (); If (first. floor = B) {return first. TME;} next_up.floor = first. FLOOR + Kp [first. floor]; If (next_up.floor <= N &&! Mark [next_up.floor]) {next_up.tme = first. TME + 1; Mark [next_up.floor] = true; q. push (next_up);} next_down.floor = first. floor-Kp [first. floor]; If (next_down.floor> = 1 &&! Mark [next_down.floor]) {next_down.tme = first. TME + 1; Mark [next_down.floor] = true; q. push (next_down) ;}} return-1 ;}int main () {While (~ Scanf ("% d", & N), n) {scanf ("% d", & A, & B); For (INT I = 1; I <= N; I ++) {scanf ("% d", & Kp [I]) ;}// input int res = BFS (); printf ("% d \ n", Res) ;}</span>



Hangdian 1548 a strange lift (Guang suo)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.