HDU 1548.A strange lift

A strange lift

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

SubmitStatusPracticeHDU 1548

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

BFS题目

直接套用模板即可

其中有一点是如果"DOWN"到负数楼层,则不下降层数,而非降至1楼;"UP"同理(我觉得没人会读错吧……)

 1 /*
 2 By:OhYee
 3 Github:OhYee
 4 Email:[email protected]
 5 Blog:http://www.cnblogs.com/ohyee/
 6
 7 かしこいかわいい?
 8 エリーチカ!
 9 要写出来Хорошо的代码哦~
10 */
11
12 #include <cstdio>
13 #include <algorithm>
14 #include <cstring>
15 #include <cmath>
16 #include <string>
17 #include <iostream>
18 #include <vector>
19 #include <list>
20 #include <queue>
21 #include <stack>
22 using namespace std;
23
24 //DEBUG MODE
25 #define debug 0
26
27 //循环
28 #define REP(n) for(int o=0;o<n;o++)
29
30 const int maxn = 205;
31 int k[maxn];
32 int N;
33
34 int BFS(int s,int v) {
35     if(s == v)
36         return 0;
37
38     queue<int> Q;
39     bool visited[maxn];
40     memset(visited,false,sizeof(visited));
41     int dis[maxn];
42     memset(dis,0,sizeof(dis));
43
44     Q.push(s);
45     visited[s] = true;
46     while(!Q.empty()) {
47         int th = Q.front();
48         Q.pop();
49
50         //达到终点
51         if(th == v)
52             break;
53
54         //拓展节点
55         int next;
56         for(int i = -1;i == -1 || i == 1;i += 2) {
57             next = th + i * k[th];
58             if(next > N || next <= 0)
59                 continue;
60             if(!visited[next]) {
61                 Q.push(next);
62                 visited[next] = true;
63                 dis[next] = dis[th] + 1;
64             }
65         }
66
67     }
68
69     if(dis[v])
70         return dis[v];
71     else
72         return -1;
73 }
74
75 bool Do() {
76     int s,v;
77     if(scanf("%d%d%d",&N,&s,&v),N == 0)
78         return false;
79     REP(N)
80         scanf("%d",&k[o + 1]);
81     printf("%d\n",BFS(s,v));
82     return true;
83 }
84
85 int main() {
86     while(Do());
87     return 0;
88 }
时间: 2025-01-22 07:30:50

HDU 1548.A strange lift的相关文章

HDU 1548 A strange lift(Dijkstra,简单BFS)

题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数,然后是n个数分别代表第i层的移动范围.输出最少移动次数,若不可达,输出-1. 解题思路: 1.用Dijkstra算法,首先构建邻接矩阵,注意在构造时,要考虑i-k[i]<1和i+k[i]>n,i代表当前所在层. 1 #include<string.h> 2 #include<st

hdu 1548 A strange lift (dijkstra算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目大意:升降电梯,先给出n层楼,然后给出起始的位置,即使输出从A楼道B楼的最短时间. 注意的几点 (1)每次按一下,只能表示上或者是下,然后根据输入的看是上几层或者是下几层. (2)注意不能到底不存在的楼层. 详见代码. 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 const int IN

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): 11341    Accepted Submission(s): 4289 Problem Description There is a strange lift.The lift can stop can at every floor as you want

HDU 1548 A strange lift【不错的最短路,spfa】

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21281    Accepted Submission(s): 7786 Problem Description There is a strange lift.The lift can stop can at every floor as you want

HDU 1548 A strange lift(最短路&amp;&amp;bfs)

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26943    Accepted Submission(s): 9699 Problem Description There is a strange lift.The lift can stop can at every floor as you want,

hdu 1548 A strange lift Dijkstra+SPFA算法AC

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13777    Accepted Submission(s): 5254 Problem Description There is a strange lift.The lift can stop can at every floor as you want

hdu 1548 A strange lift (dijkstra)

A strange liftTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 32529    Accepted Submission(s): 11664 Problem DescriptionThere is a strange lift.The lift can stop can at every floor as you want,

Hdu 1548 A strange lift(BFS)

Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1548 一道简单的bfs,适合新手.你现在所在的电梯层为一个节点,而你从这个节点可以拜访另外两个节点(电梯向上走为一个节点,电梯向下走有一个节点),而拜访的时候自然也要避免拜访重复,否则会陷入死循环. #include <iostream> #include <queue> using namespace std; const int maxn = 200+10; int N,

HDU 1548 A strange lift——BFS

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <queue> 7 #define sc(x) scanf("%d",&(x)) 8 #define pf(x) printf("%d\n", x) 9 #def