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. [ NOI 2001 ] 方程的解数

    \(\\\) \(Description\) 已知一个 \(N\) 元高次方程: \[ k_1x_1^{p_1}+k_2x_2^{p_2}+...+k_nx_n^{p_n}=0 \] 要求所有的 \( ...

  2. Java:一个简捷的可分页的ResultSet实现

    内容 前言 JDBC和分页 和具体数据库相关的实现方法 另一种繁琐的实现方法 使用Vector进行分页 一个新的Pageable接口及其实现 Pageable的使用方法 总结 参考资料 关于作者 前言 ...

  3. org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: Property [xxx] not readable on type [xxx]

    由于javaBean中的属性是custFullName,所以在使用jsp的时候,通过el表达式获取属性的值<td>${m.CustFullName}</td>.但是加载页面的时 ...

  4. MySql(四)Select条件查询

    select条件查询的格式如下: SELECT 查询列表FROM 表名WHERE 筛选条件:123456根据筛选条件可以分为以下几类: 按照条件按表达式进行筛选 常用条件运算符如下:> .< ...

  5. 微服务网关从零搭建——(六)ocelot配置追踪功能

    butterfly 准备工作 首先下载buterfly release版本 解压并通过命令启动:dotnet Butterfly.Web.dll --EnableHttpCollector=true ...

  6. jQuey中的return false作用是什么?

    jQuey中的return false作用是什么?在众多的语句中都有return false的使用,当然对于熟悉它的开发者来说,当然是知根知底,知道此语句的作用,当然也就知道在什么时候使用此语句,不过 ...

  7. P2041 分裂游戏

    P2041 分裂游戏 手推$n=3$是无解的,推断$n>=3$是无解的 证明略,这是道结论题. #include<iostream> #include<cstdio> # ...

  8. Ztree加载完成后显示勾选节点

    ①前言:这个在度娘上没有搜到解决的办法,于是自己查看了ztree的API,发现其实还是比较简单的.做个笔记以备不时之需. ②需求: 像下图一样,在加载完成之后就显示需要勾选的项. ③解决方案: 首先页 ...

  9. importdata-- matlab

    source file: test.dat *************************** Day1  Day2  Day3  Day4  Day5  Day6  Day795.01 76.2 ...

  10. [已解决]运行gunicorn失败:[ERROR] Connection in use 127.0.0.1 8080

    最近重新部署了一下应用程序,之后重新运行gunicorn,使用如下命令: gunicorn -b 0.0.0.0:8000 manage:app --reload 之后出现了一堆错误,具体错误内容如下 ...