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. 聊天系统Demo,增加文件传送功能(附源码)-- ESFramework 4.0 快速上手(14)

    本文我们将介绍在ESFramework 4.0 快速上手(08) -- 入门Demo,一个简单的IM系统(附源码)的基础上,增加文件传送的功能.如果不了解如何使用ESFramework提供的文件传送功 ...

  2. PHP中使用CURL(三)

    对 post 提交的数据进行 http_build_query处理,然后再send出去,能实现更好的兼容性,更小的请求数据包. <?php /** * PHP发送Post数据 * @param ...

  3. 使用InternetReadFile时要注意读取完整数据

    使用这个api读取数据后,即使数据不完整,该函数不会报任何错,GetLastError也显示操作成功.使用时要注意通过其参数判断是否读取完毕 void InternetReadFileEx(HINTE ...

  4. Chapter 16_4 私密性

    在Lua面向对象编程的基础设计当中,没有提供私密性机制.但是可以用其他方法实现,从而获得对象的访问控制. 这种实现不常用,作为兴趣爱好,只做基本了解. 基本做法是:通过两个table来表示一个对象.一 ...

  5. Html加载swf 兼容IE8 (含以下)显示

    嵌入参数说明: 1,AllowScriptAccess  参数: sameDomain:仅当 SWF 文件和网页位于同一域中时才允许执行外出脚本访问.这是 AVM2 内容的默认值----播放网络视频, ...

  6. 获取当前设备的IP地址

    头文件: #import <ifaddrs.h> #import <arpa/inet.h> #import <net/if.h> 宏定义: #define IOS ...

  7. redhat 安装hadoop1.2.1伪分布式

    完整安装过程参考:http://www.cnblogs.com/shishanyuan/p/4147580.html 一.环境准备    1.安装linux.jdk      2.下载hadoop2. ...

  8. ubuntu/linux 下 git 通过代理下载数据 (最简单的方式)

    git国内访问较慢,走代理较快. 方法:git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:port_n ...

  9. MFC连接Access讲解(3合1) .

    方法一: 1.首先,要用#import语句来引用支持ADO的组件类型库(*.tlb),其中类型库可以作为可执行程序(DLL.EXE等)的一部分被定位在其自身程序中的附属资源里,如:被定位在msado1 ...

  10. JRE与JDK

    Java源代码是以*.java的纯文本文件,可以使用任何文本编辑器编写,但不可以执行. JDK是Java语言的开发包,可以将*.java文件编译成可执行Java文件. 可执行Java程序需要JVM才可 ...