题目: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学习经历24

    Fragment生命周期 1.onAttach() 2.onCreate() 3.onCreateView() 4.onActivityCreated() 5.onStart() 6.onResume ...

  2. 动态加载JS

    <script> Date.prototype.format = function(format) { var o = { , //month "d+" : this. ...

  3. c++ stl string char* 向 string 转换的问题

    请看下面代码 string AddString(const string& a,const string & b) { return a + b; } int _tmain(int a ...

  4. linux、windows下导入、导出mysql数据库命令

    一.导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径): 1.导出数据和表结构:[不是mysql里的命令]mysqldump -u用户名 -p密码 数据库名 > 数据 ...

  5. Practical Java

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. SQL判断临时表是否存在

    IF EXISTS(select * from tempdb..sysobjects where id=object_id('tempdb..#tb')) BEGIN DROP TABLE #tb E ...

  7. MySQL多表更新(逻辑外键/事实外键)

    语法结构: UPDATE  table_reference  SET  列名1=value1[,列名2=value2,......] [WHERE  where_condition] 说明: tabl ...

  8. 线程入门之yield

    package com.thread; /** * <yiedl:把cpu让给其他线程> * <功能详细描述> * * @author 95Yang */ public cla ...

  9. mysql 2003 10038 连接不上的解决

    网上写的很复杂,其实解决办法是 你在右键管理员权限下运行 mysqld. 忘记这个了,害的又找了近1个小时的时间找问题.

  10. mysq错误(1)空用户创建库

    mysql5.6.24免安装版: 1.ERROR 1044 (42000): Access denied for user ''@'localhost' to database 现象:创建库失败. 出 ...