专题二 第七道题和第八道题

1.题目编号:1013和1014

2.简单题意:有一个奇怪的电梯,电梯可以停在你想停的任意一层,每一层都有一个数k[i],并且每一层只有向上和向下两个按钮,当你在i层时你按向上的按钮它会向上送你k[i]层,你将会到达第i+k[i]层,如果你按向下则会向下k[i]层,你将会到达第i-k[i]层,当然了,电梯不会上升到比n层还高也不会低于第1层。给你三个数N,A,B,然后是N个k[i],N为建筑物的高度,A为你开始在的层数,B为你最后要到达的层数,要求求出最少需要按几次。

3.解题思路形成过程:首先看到这个题就想到广度搜索,首先得定义一个数据类型,存放到几层之后走了几步,还要标记已经走过的层,在广度搜索中当队列非空时先搜向下的,然后向上的,若空了还没有到达要到的层就返回-1

4.感悟:感觉好像做过这个题,但好像貌似不是用这种方法做的……忘记了

5.AC的代码:

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int k[201],mark[201];
int A,B,N;
struct Record
{
  int lift,step;
};
int bfs()
{
  Record a,b;
  queue <Record> q;
  memset(mark,0,sizeof(mark));//没走过的层都为0
  a.lift=A;
  a.step=0;
  mark[a.lift]=1;
  q.push(a);
  while(!q.empty())
  {
    a=q.front();
    q.pop();
    if(a.lift==B)
    return a.step;// 到达终点  直接返回步数
    b.lift=a.lift-k[a.lift];
    if(b.lift>0 && b.lift<=N && !mark[b.lift])
    {
      b.step=a.step+1;
      mark[a.lift]=1;
      q.push(b);
    }
    b.lift=a.lift+k[a.lift];
    if(b.lift>0 && b.lift<=N && !mark[b.lift])
    {
      mark[a.lift]=1;
      b.step=a.step+1;
      q.push(b);
    }
  }
  return -1;
}

int main()
{
  int i;
  while(cin>>N&&N)
  {
    cin>>A>>B;
    for(i=1;i<=N;++i)
    cin>>k[i];
    cout<<bfs()<<endl;
  }
  return 0;
}

原题:

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 &lt;= Ki &lt;= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button &quot;UP&quot; , 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 &quot;DOWN&quot; , 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 &quot;UP&quot;, and you'll go up to the 4 th floor,and if you press the button &quot;DOWN&quot;, 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.<br>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 &quot;UP&quot; or &quot;DOWN&quot;?<br>


 


Input


The input consists of several test cases.,Each test case contains two lines.<br>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.<br>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<br>3 3 1 2 5<br>0


 


Sample Output


  
  
3




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值