http://acm.hdu.edu.cn/showproblem.php?pid=1548

A strange lift

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

Total Submission(s): 11341    Accepted Submission(s): 4289

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 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
 AC代码:

<span style="font-size:24px;">#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std; const int maxn = 201;
int n, a, b;
int kp[maxn];
bool mark[maxn];
struct Node {
int floor;
int tme;
}; int BFS() {
memset(mark, false, sizeof(mark));
queue<Node> Q;
Node first, next_up, next_down;
first.tme = 0;
first.floor = a;
Q.push(first);
mark[a] = true; //标记 while (!Q.empty()) {
first = Q.front();
Q.pop(); if (first.floor == b) {
return first.tme;
}
next_up.floor = first.floor + kp[first.floor];
if (next_up.floor <= n && !mark[next_up.floor ]) {
next_up.tme = first.tme + 1;
mark[next_up.floor] = true;
Q.push(next_up);
}
next_down.floor = first.floor - kp[first.floor];
if (next_down.floor >= 1 && !mark[next_down.floor]) {
next_down.tme = first.tme + 1;
mark[next_down.floor] = true;
Q.push(next_down);
}
}
return -1;
} int main() { while (~scanf("%d", &n), n ) {
scanf("%d%d", &a, &b);
for (int i = 1; i <= n; i++) {
scanf("%d", &kp[i]);
} //输入
int res = BFS();
printf("%d\n", res);
}
}</span>

杭电 1548 A strange lift(广搜)的更多相关文章

  1. 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 ...

  2. hdu 1548 A strange lift

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...

  3. HDU 1548 A strange lift (广搜)

    题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...

  4. 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 ...

  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 (Dijkstra)

    A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...

  7. hdu 1548 A strange lift (bfs)

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

  8. HDU 1548 A strange lift (最短路/Dijkstra)

    题目链接: 传送门 A strange lift Time Limit: 1000MS     Memory Limit: 32768 K Description There is a strange ...

  9. HDU 1548 A strange lift 搜索

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

随机推荐

  1. 滚动时sticky nav

    参考w3c <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <met ...

  2. (3)左右值再探与decltype

    Decltype 类型指示符 “引用从来都作为其所指对象的同义词出现,只有用在decltype处是一个例外” 理解: Decltype和auto区别: 1.     auto是从表达式类型推断出要定义 ...

  3. Android 读取asset文件

    * * 从Assets中读取图片 */ private Bitmap getImageFromAssetsFile(String fileName) { Bitmap image = null; As ...

  4. POJ_2195_Going Home

    题意:用'H','m','.'作出矩阵,'H'代表房子,'m'代表人,人一次只能水平或者垂直移动到相邻的点,问所有人一共走的步数的最小值. 分析:明显的求二分图最大权匹配.KM算法求得的是最大权匹配, ...

  5. Android开发使用控件入门--环境搭建

    Android开发使用控件入门--环境搭建 软件名称(,梦,,想.CAD  ,控件) 1. 环境搭建: 3 1.1. 安装Eclipse 3 1.2. 下载JDK 3 1.3. 下载Android S ...

  6. (转)vim编辑器操作命令大全-绝对全

    周六了,熟悉熟悉vim 命令 学习链接: vim命令大全 http://blog.csdn.net/scaleqiao/article/details/45153379 vim命令小技巧 http:/ ...

  7. Python Web开发

    参考原文 Python廖雪峰 WSGI接口 WSGI(Web Server Gateway Interface)是一个接口,用来屏蔽底部的细节(如TCP的建立连接,HTTP原始请求和响应格式等).WS ...

  8. JAVA基础——文件File简单实用

    1.1java.io.File File用于表示文件系统中的一个文件或目录 通过File可以: 1:访问该文件或目录的属性信息(名字,大小,修改时间等) file.getName();获取文件名fil ...

  9. 实验十二:SWING界面设计

    实验程序: import java.awt.FlowLayout;import javax.swing.*;import java.awt.Container;public class jianli ...

  10. JS中遍历EL表达式中后台传过来的Java集合

    前言:在我的项目里有这么一个情况,后台直接model.addAttribute()存储了一个对象,此对象内部有一个集合,前端JSP处理的方法正常情况下就是直接使用EL表达式即可.但是如果在JS中需要使 ...