题目: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. MFC之简单计算器

    1.界面 2.变量 combobox的变量类型是CComBoBox类型,三个输入框是double类型: 它的type是Drop List 3.代码 (1).初始化combobox BOOL Ccalc ...

  2. 异常详细信息: System.Web.Hosting.HostingEnvironmentException: 访问 IIS 元数据库失败 解决方法

    访问IIS元数据库失败 同理,给操作系统的新建用户赋予IIS操作权限同样可以采用该命令来处理 说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错 ...

  3. 【leetcode❤python】 257. Binary Tree Paths

    深度优先搜索 # Definition for a binary tree node.# class TreeNode:#     def __init__(self, x):#         se ...

  4. 【leetcode❤python】 Maximum Depth of Binary Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  5. Java自制人机小游戏——————————剪刀、石头、布

    package com.hello.test; import java.util.Scanner; public class TestGame { public static void main(St ...

  6. Servlet上下文

    Servlet上下文 运行在Java虚拟机的每一个Web应用程序都有一个与之相关的Servlet上下文. Java Servlet API提供了一个ServletContext接口来表示上下文.在这个 ...

  7. XML详解:第二部分 XML Schema

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

  8. android测试参考,及CreateProcess failure, error问题解决

    今天小伙伴问我问题,我给了这2个小命令,或许做android测试的同学可以用得着. 截图命令adb shell /system/bin/screencap -p /sdcard/screenshot. ...

  9. ToStringBuilder 学习

    一.简介与引入   1.ToStringBuilder.HashCodeBuilder.EqualsBuilder.ToStringStyle.ReflectionToStringBuilder.Co ...

  10. C++指针内存

    这是一个关于C++指针的问题,思考了一下 void GetMemory(char *p, int num){ p = (char*) malloc (sizeof(char) * num); } vo ...