题目:http://acm.hdu.edu.cn/showproblem.php?pid=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
 
题目大意:一共有两个方向的选择,向上or向下,若up则到i+k[i]层的位置,若down,则到i-k[i]的位置,找到最短的从A到B的跳转数
 
总结:注意搜索后需要标记,最好申明一个标记数组进行标记,其次考虑如何剪枝,注意边界的范围。
 
 #include<iostream>
#include<queue> using namespace std; int k[],flag[];
int n,a,b; struct node{
int floor;
int step;
}; int bfs(){
node cur,next;
queue<node> q;
cur.floor = a;
cur.step = ;
flag[a] = ;//最开始把这个给忘记了还WA了一次,我傻!
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
if(cur.floor == b){
return cur.step;
}
if(cur.floor+k[cur.floor]<=n && cur.floor+k[cur.floor]> && !flag[cur.floor+k[cur.floor]]){
next.floor = cur.floor + k[cur.floor];
flag[cur.floor] = ;
next.step = cur.step + ;
q.push(next);
}
if(cur.floor-k[cur.floor]> && cur.floor-k[cur.floor]<=n && !flag[cur.floor-k[cur.floor]]){
next.floor = cur.floor - k[cur.floor];
flag[cur.floor] = ;
next.step = cur.step + ;
q.push(next);
}
}
return -;
} int main(){
while(cin>>n,n){
cin>>a>>b;
for(int i=;i<=n;i++){
cin>>k[i];
flag[i]=;
}
cout<<bfs()<<endl;
}
return ;
}

bfs A strange lift的更多相关文章

  1. HDU 1548 A strange lift (bfs / 最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...

  2. hdu 1548 A strange lift 宽搜bfs+优先队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...

  3. hdu 1548 A strange lift (bfs)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  4. HDU1548- A strange lift (BFS入门)

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A Strrange lift Time Limit: 2000/1000 MS (Java/ ...

  5. HDU-1548 A strange lift(单源最短路 或 BFS)

    Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...

  6. HDU 1548 A strange lift(BFS)

    Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...

  7. E - A strange lift 【数值型BFS+上下方向】

    There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 ...

  8. HDU 1548 A strange lift(最短路&&bfs)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  9. A strange lift

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

随机推荐

  1. android导入项目出现style错误,menu错误

    android导入项目出现style错误,menu错误 style //查看 res/values/styles.xml 下的报错点. <style name="AppBaseThem ...

  2. SqlSever基础 detalength函数 字节的个数

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  3. UE4编程之C++创建一个FPS工程(二)角色网格、动画、HUD、子弹类

    转自:http://blog.csdn.net/u011707076/article/details/44243103 紧接上回,本篇文章将和大家一同整理总结UE4关于角色网格.动画.子弹类和HUD的 ...

  4. Why And When To Use Pre-Update and Pre-Insert Triggers In Oracle Forms

    Whenever we commit after entering data in Oracle Forms, many triggers fires during this event and al ...

  5. Django基础-过滤器

    1.可以通过过滤器来修改变量的显示,过滤器的形式是:{{ variable | filter }},管道符号'|'代表使用过滤器 2.过滤器能够采用链式的方式使用,例如:{{ text | escap ...

  6. Linux链接库一(动态库,静态库,库放在什么路径下)

    http://www.cppblog.com/wolf/articles/74928.html http://www.cppblog.com/wolf/articles/77828.html http ...

  7. IIS网站部署注意点

    在IIS上部署网站时,除了在添加网站时配置好相关程序池,主目录,安全性,选择.Netframwork版本这些步骤外, 容易忘记的是有些网站需要打开web服务扩展.

  8. SQL生成包含年月日的流水号

    --************************************************************************************************** ...

  9. JVM 1.类的加载、连接、初始化

    Java类的加载是由类加载器来完成的,过程如下: 首先,加载是把硬盘.网络.数据库等的class文件中的二进制数据加载到内存的过程,然后会在Java虚拟机的运行时数据区的堆区创建一个Class对象,用 ...

  10. 线程入门之join方法

    package com.thread; /** * <join:将某线程加入进来,相当于方法调用,也叫合并某个线程> * <功能详细描述> * * @author 95Yang ...