HDU_1548

Posted 夏午晴天

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU_1548相关的知识,希望对你有一定的参考价值。

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

1.dijkstra解法

 1 #include <iostream>
 2 #include <string.h>
 3 using namespace std;
 4 #define MAXN 250
 5 const int INF = 1234567;
 6 int g[MAXN][MAXN];
 7 int dis[MAXN];
 8 int vis[MAXN];
 9 int n;
10 // 5 1 5
11 // 3 3 1 2 5
12 // 0
13 void init(){
14     memset(vis,0,sizeof(vis));
15     for(int i = 1; i <= n;i++){
16         for(int j = 1; j <= n; j++){
17             if(i == j){
18                 g[i][j] = 0;
19             }
20             else g[i][j] = INF;
21         }
22     }
23 }
24 void dij(int v0){
25     int pos = v0;
26     for(int i = 1; i <= n; i++){
27         dis[i] = g[v0][i];
28     }
29     vis[pos] = 1;
30     for(int i = 1; i < n; i++){
31         int mins = INF;
32         for(int j = 1; j <= n; j++){
33             if(!vis[j] && dis[j] < mins){
34                 pos = j;
35                 mins = dis[j];
36             }
37         }
38         vis[pos] = 1;
39         for(int j = 1; j <= n; j++){
40             if(!vis[j] && dis[j] > dis[pos] + g[pos][j])
41                 dis[j] = dis[pos] + g[pos][j];
42         }
43     }
44 }
45 int main(){
46     while(cin >> n && n){
47         init();
48         int from, to;
49         cin >> from >> to;
50         for(int i = 1; i <= n; i++){
51             int w;
52             cin >> w;
53             if(w + i <= n)
54                 g[i][w + i] = 1;
55             if(i - w >= 0)
56                 g[i][i - w] = 1;
57         }
58         dij(from);
59         if(dis[to] != INF)
60             cout << dis[to] << endl;
61         else cout << -1 << endl;
62     }
63 }


2. bfs解法

 

 1 #include <iostream>
 2 #include <queue>
 3 #include <string.h>
 4 using namespace std;
 5 const int MAXN = 250;
 6 int n;
 7 int from, to;
 8 int k[MAXN];
 9 int vis[MAXN];
10 // 5 1 5
11 // 3 3 1 2 5
12 // 0
13 struct Node{
14     int x, step;
15 }pos,q;
16 
17 void bfs(int start){
18     memset(vis,0,sizeof(vis));
19     queue<Node> que;
20     pos.x = start;
21     pos.step = 0;
22     que.push(pos);
23     vis[start] = 1;
24     while(!que.empty()){
25         pos = que.front();
26         que.pop();
27         if(pos.x == to){
28             cout << pos.step << endl;
29             return ;
30         }
31         q.x = pos.x - k[pos.x];
32         if(q.x >= 1 && vis[q.x] == 0){
33             q.step = pos.step + 1;
34             que.push(q);
35             vis[q.x] = 1;
36         }
37         q.x = pos.x + k[pos.x];
38         if(q.x <= n && vis[q.x] == 0){
39             q.step = pos.step + 1;
40             que.push(q);
41             vis[q.x] = 1;
42         }
43     }
44     cout << -1 << endl;
45 }
46 int main(){
47     while(cin >> n && n){
48         cin >> from >> to;
49         for(int i = 1; i <= n; i++){
50             cin >> k[i];
51         }
52         bfs(from);
53     }
54     return 0;
55 }

 

以上是关于HDU_1548的主要内容,如果未能解决你的问题,请参考以下文章

hdu1548 A strange lift(bfs)

hdu-1548 A strange lift

HDU 1548 A strange lift

HDU 1548 A strange lift

cogs 364. [HDU 1548] 奇怪的电梯 Dijkstra

HDU 1548 A strange lift (暴力搜索)