hdu1548 A Strange Lift (simple BFS)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1548

A Strange Lift Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 15974 Accepted Submission (s): 5973


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

Test instructions

A strange elevator, each layer has a number of ki, in each layer there are two choices, choose to Rise Ki layer, or Drop KI layer, now give you the starting layer and the target layer, ask at least several times to operate the elevator can reach the target layer

If not, then output-1;


"Tips"

The reason why I wa is not thoughtful, forget to test when the starting layer and the target layer is the same situation.

"BFS Code"

#include <iostream> #include <queue> #include <cstring>using namespace Std;int k[250];int t;int A, B; BOOL Vis[250];struct node{int x,times;} No;int BFS (int a) {Queue<node>q;while (! Q.empty ()) Q.pop (); no.x=a;no.times=0; Q.push (no); Vis[no.x]=1;while (! Q.empty ()) {node Now=q.front (); Q.pop (); if (now.x==b) return now.times; AC for (int i=0;i<2;i++) {node next;if (i==0) {next.x=now.x+k[now.x-1] at the beginning with the judging condition;  Rise}else if (i==1) {next.x=now.x-k[now.x-1];//Descent}if (Next.x>=1&&next.x<=t&&!vis[next.x]) { Next.times=now.times+1;vis[next.x]=1; The number of layers reached is directly labeled, because if you go back to that layer you loop the IF (next.x==b) return Next.times;else{q.push (next);}}} return-1;} int main () {while (cin>>t&&t) {memset (vis,0,sizeof (Vis)); Cin>>a>>b;for (int i=0;i<t;i++ ) {cin>>k[i];} int Ans=bfs (A); Cout<<ans<<endl;} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu1548 A Strange Lift (simple BFS)

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.