posts - 129,comments - 0,views - 37643
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 is on floor A,and you want to go to floor B,how many times at least he havt 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即可,但小心a==b这种极其恶心的数据!!

复制代码
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <queue>
 5 using namespace std;
 6 
 7 queue<int> q;
 8 int a[210],aa,b,n,s,l,step=0;
 9 bool f[210];
10 
11 void bfs()
12 {
13 if (aa==b) {
14     printf("0\n");
15     return;
16 }
17 step=0;
18 while (!q.empty())
19     q.pop();
20 f[aa]=true;
21 q.push(aa);
22    while (!q.empty())
23     {
24         step++;
25         l=q.size();
26         for (int i=0;i<l;i++)
27         {
28             s=q.front();
29             q.pop();
30         //    printf("s:%d\n",s);
31             if (s-a[s]==b || s+a[s]==b) 
32             {
33             //    printf("STEEEEPPP:%d\n",step);
34                printf("%d\n",step);
35                 return;
36             }
37                if (s-a[s]>=1 && not f[s-a[s]])
38                 {
39                     f[s-a[s]]=true;
40                     q.push(s-a[s]);
41                 }
42                if (s+a[s]<=n && not f[s+a[s]])
43                 {
44                     f[s+a[s]]=true;
45                     q.push(s+a[s]);
46                 }
47         }
48     }
49 printf("-1\n");
50 }
51 
52 
53 int main ()
54 {
55    while (scanf("%d%d%d",&n,&aa,&b)!=EOF)
56     {
57         if (n==0) break;
58         memset(f,false,sizeof(f));
59         for (int i=1;i<=n;i++)
60             scanf("%d",&a[i]);
61         bfs();
62     }
63 return 0;
64 }
复制代码

 

posted on   cssystem  阅读(119)  评论(0编辑  收藏  举报
编辑推荐:
· 电商平台中订单未支付过期如何实现自动关单?
· 用 .NET NativeAOT 构建完全 distroless 的静态链接应用
· 为什么构造函数需要尽可能的简单
· 探秘 MySQL 索引底层原理,解锁数据库优化的关键密码(下)
· 大模型 Token 究竟是啥:图解大模型Token
阅读排行:
· 瞧瞧别人家的限流,那叫一个优雅!
· 1.net core 工作流WorkFlow流程(介绍)
· 一文彻底搞懂 MCP:AI 大模型的标准化工具箱
· 面试官:如果某个业务量突然提升100倍QPS你会怎么做?
· 短信接口被刷爆:我用Nginx临时止血
< 2012年12月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示