码迷,mamicode.com
首页 > 其他好文 > 详细

深度优先搜索:奇怪的电梯

时间:2015-04-15 21:20:22      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

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

技术分享
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <string>
 4 #include <cstring>
 5 #include <queue>
 6 
 7 using namespace std;
 8 
 9 const int MAX = 256;
10 const int x[] = { 1, -1 };
11 
12 struct Node
13 {
14     int floor, cut;
15     Node(){};
16     Node(int _floor, int _cut) : floor(_floor), cut(_cut){};
17 };
18 
19 int n, a, b;
20 
21 int k[MAX];
22 bool vis[MAX];
23 queue <Node> que;
24 
25 void BFS();
26 
27 int main()
28 {
29 #ifdef OFFLINE
30     freopen("in.txt", "r", stdin);
31     freopen("out.txt", "w", stdout);
32 #endif
33 
34     while (scanf("%d", &n) != EOF && n != 0)
35     {
36         scanf("%d%d", &a, &b);
37 
38         memset(k, 0, sizeof(k));
39         memset(vis, false, sizeof(vis));
40         for (int i = 1; i <= n; i++)
41         {
42             scanf("%d", &k[i]);
43         }
44 
45         BFS();
46     }
47 
48     return 0;
49 }
50 
51 void BFS()
52 {
53     while (!que.empty()) que.pop();
54     que.push(Node(a, 0));
55     vis[a] = true;
56     
57     while (!que.empty())
58     {
59         Node cutNode = que.front();
60 
61         if (cutNode.floor == b)
62         {
63             printf("%d\n", cutNode.cut);
64             return;
65         }
66 
67         for (int i = 0; i < 2; i++)
68         {
69             int nextfloor = cutNode.floor + x[i] * k[cutNode.floor];
70             if (nextfloor >= 1 && nextfloor <= n && !vis[nextfloor])
71             {
72                 que.push(Node(nextfloor, cutNode.cut + 1));
73                 vis[nextfloor] = true;
74             }
75         }
76 
77         que.pop();
78     }
79 
80     printf("-1\n");
81     return;
82 }
View Code

 

 

深度优先搜索:奇怪的电梯

标签:

原文地址:http://www.cnblogs.com/gwsbhqt/p/4430081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!