HDU 1548 A strange lift 题解

A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33697    Accepted Submission(s): 12038

Problem Description
There 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"?
 


Input
The 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.
 


Output
For 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 5 3 3 1 2 5 0
 


Sample Output
3
 


Recommend
8600   |   We have carefully selected several similar problems for you:  1385 1142 1217 2066 2544 
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
本题为典型的bfs模板题
此处采用了一位的深度优先搜索
规定了方向
下面上我加上完全的注释
谁都能看懂的代码
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1 //Author:LanceYu
 2 #include<iostream>
 3 #include<string>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<fstream>
 7 #include<iosfwd>
 8 #include<sstream>
 9 #include<fstream>
10 #include<cwchar>
11 #include<iomanip>
12 #include<ostream>
13 #include<vector>
14 #include<cstdlib>
15 #include<queue>
16 #include<set>
17 #include<ctime>
18 #include<algorithm>
19 #include<complex>
20 #include<cmath>
21 #include<valarray>
22 #include<bitset>
23 #include<iterator>
24 #define ll long long
25 using namespace std;
26 const double clf=1e-8;
27 //const double e=2.718281828;
28 const double PI=3.141592653589793;
29 const int MMAX=2147483647;
30 //priority_queue<int>p;
31 //priority_queue<int,vector<int>,greater<int> >pq;
32 struct node
33 {
34     int x,step;
35 };
36 int n,start,last;
37 int dir[2][201],vis[201];//dir分为两个方向,向上和向下 
38 int bfs(int start,int last)
39 {
40     int i;
41     queue<node> q;
42     q.push(node{start,0});
43     vis[start]=1;
44     while(!q.empty())
45     {
46         node t=q.front();
47         q.pop();
48         if(t.x==last)
49             return t.step;
50         for(int i=0;i<2;i++)
51         {
52             int dx=t.x+dir[i][t.x];
53             if(dx>=0&&dx<n&&!vis[dx])//一维深度搜索 
54             {
55                 vis[dx]=1;
56                 q.push(node{dx,t.step+1});
57             }
58         }
59     }
60     return -1;
61 }
62 int main()
63 {
64     int temp;
65     while(scanf("%d",&n)!=EOF)//这里输入要分开,因为输入为0时停止 
66     {
67         if(n==0)
68             return 0;
69         scanf("%d%d",&start,&last);
70         memset(vis,0,sizeof(vis));
71         for(int i=0;i<n;i++)//每一层的数据录入,包括两个方向 
72         {
73             scanf("%d",&temp);
74             dir[0][i]=temp;
75             dir[1][i]=-temp;
76         }
77         int ans=bfs(start-1,last-1);//注意此处要-1,受到数组的限制和影响 
78             printf("%d\n",ans);
79     }
80     return 0;
81 }

2018-11-16  01:05:38  Author:LanceYu

posted @ 2018-11-16 01:07  微生泽驰  阅读(289)  评论(1编辑  收藏  举报