HDU 1548- A strange lift

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"? 

InputThe 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.OutputFor 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搜索即可;对于走过的楼层打上标记,记录走的步数;
参考代码:
复制代码
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<cstdlib>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<stack>
 9 #include<set>
10 #include<vector>
11 #include<map>
12 using namespace std;
13 #define PI acos(-1)
14 #define EPS 1e-8
15 typedef pair<int,int> PII;
16 typedef long long LL;
17 const int INF=0x3f3f3f3f;
18 const LL inf=0x3f3f3f3f3f3f3f3fLL;
19 const int maxn=1e5+10;
20 struct Node{
21     int Floor;
22     int step;
23 } node;
24 int a[maxn],v[maxn],n,s,e;
25 int bfs()
26 {
27     queue<Node> q;
28     node.Floor=s,node.step=0;
29     q.push(node);v[node.Floor]=1;
30     while(!q.empty())
31     {
32         Node tem=q.front(); q.pop();
33         Node tp;
34         if(tem.Floor==e) return tem.step;   
35         for(int i=0; i<2; i++)
36         {
37             if(i==0) tp.Floor=tem.Floor+a[tem.Floor-1];
38             else tp.Floor=tem.Floor-a[tem.Floor-1];
39             if(tp.Floor<1||tp.Floor>n||v[tp.Floor]) continue;
40             tp.step=tem.step+1;
41             q.push(tp);
42             v[tp.Floor]=1;
43         }
44     }
45     return -1;
46 }
47 int main()
48 {
49     while(~scanf("%d",&n) && n)
50     {
51         scanf("%d %d",&s,&e);
52         memset(a,0,sizeof(a));
53         memset(v,0,sizeof(v));
54         for(int i=0; i<n; i++) scanf("%d",&a[i]);
55         printf("%d\n",bfs());
56     }
57     return 0;
58 }
View Code
复制代码

 

posted @   StarHai  阅读(186)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· MQ 如何保证数据一致性?
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
点击右上角即可分享
微信分享提示