A strange lift

 
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 945 Accepted Submission(s): 450
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
is on floor A,and you want to go to floor B,how many times at least he
havt 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
 

 
 
Recommend
8600

做地图搜索做到恶心,终于来了道换种问法的,其实还是一样。。。。(汗。。。

这道题如果裸搜索,不剪枝的话,会超时

于是用一个vis数组标记一下此电梯是否访问到,如果访问到就无需加入到队列中(此时队列中的状态一定是从已访问的那一层得来的)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<iomanip>
#include<queue>
#define INF 0x7ffffff
#define MAXN 220
using namespace std;
const double eps=1e-;
int n,a,b;
struct node
{
int f,step;
};
int k[MAXN];
int vis[MAXN];
int bfs()
{
int x;
queue<struct node> q;
struct node pre,now;
pre.f=a;pre.step=;
q.push(pre);
vis[a]=;
while(!q.empty()){
pre=q.front();
q.pop();
if(pre.f==b)
return pre.step;
x=pre.f;
now=pre;
now.step++;
if(x+k[x]<=n&&vis[x+x[k]]==){
vis[x+x[k]]=;
now.f+=k[x];
q.push(now);
now.f-=k[x];
}
if(x-k[x]>=&&vis[x-x[k]]==){
vis[x-x[k]]=;
now.f-=k[x];
q.push(now);
}
}
return -;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie();
int res;
while(cin>>n&&n!=){
memset(vis,,sizeof(vis));
cin>>a>>b;
for(int i=;i<=n;i++){
cin>>k[i];
}
res=bfs();
if(res==-)
cout<<-<<endl;
else cout<<res<<endl;
}
}

HDU-1548--A strange lift--(BFS,剪枝)的更多相关文章

  1. hdu 1548 A strange lift (bfs)

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

  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 宽搜bfs+优先队列

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

  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(最短路&&bfs)

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

  7. HDU 1548 A strange lift (Dijkstra)

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

  8. HDU 1548 A strange lift 搜索

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

  9. HDU 1548 A strange lift (广搜)

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

  10. HDU 1548 A strange lift 题解

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

随机推荐

  1. 配置nginx为HTTPS服务器

    默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译时指定–with-http_ssl_module参数,安装模块依赖于OpenSSL库和一些引用文件,通常这些文件并不在同一个软件包中.通常这 ...

  2. Win7/Win8右键菜单管理工具(Easy Context Menu) v1.5 绿色版

    软件名称: Win7/Win8右键菜单管理工具(Easy Context Menu)软件语言: 简体中文授权方式: 免费软件运行环境: Win8 / Win7 / Vista / WinXP软件大小: ...

  3. windows服务-log4net的使用

    本文转自http://www.cnblogs.com/puzi0315/archive/2012/08/08/2628966.html Log4net监控服务状态 对于比较复杂的逻辑,可以使用log4 ...

  4. 让上下两个DIV块之间有一定距离或没有距离

    1.若想上下DIV块之间距离,只需设定:在CSS里设置DIV标签各属性参数为0div{margin:0;border:0;padding:0;}这里就设置了DIV标签CSS属性相当于初始化了DIV标签 ...

  5. Qt错误:类中使用Q_OBJECT宏导致undefined reference to vtable for "xxx::xxx"错误的原因和解决方法

    在进行Qt编程的时候,有时候会将类的定义和实现都写在源文件中,如果同时在该类中使用信号/槽,那么可能就会遇到 undefined reference to vtable for "xxx:: ...

  6. python2.7学习记录

    一.两个学习网址(2.7已经过时,建议直接学习3) http://blog.csdn.net/longxibendi/article/details/41949215 http://www.liaox ...

  7. cmd命令行

    拷贝

  8. VBS脚本操作网页元素

    =================打开百度,点击百度按钮==================== Dim btn,ieSet ie = WScript.CreateObject("Inter ...

  9. javascript中的throttle和debounce

    throttle 我们这里说的throttle就是函数节流的意思.再说的通俗一点就是函数调用的频度控制器,是连续执行时间间隔控制.主要应用的场景比如: 1.鼠标移动,mousemove 事件2.DOM ...

  10. HDU1062:Text Reverse

    Problem Description Ignatius likes to write words in reverse way. Given a single line of text which ...