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. CDOJ 1270 Playfair(模拟)

    题目链接 Playfair is a kind of substitution cipher.And the encryption role is simple.In general,there ar ...

  2. yii框架中关于控制器中filter过滤器和外部action的使用

    在yii框架中,控制器的过滤器分为执行前和执行后,这里举例是在执行控制器前的过滤. 需要在components/文件夹下定义公共的TestAction.php文件,并且实现run()方法.这个acti ...

  3. NOIP2012-普及组复赛-第一题-质因数分解

    题目描述 Description 已知正整数n是两个不同的质数的乘积,试求出两者中较大的那个质数.  输入输出格式 Input/output 输入格式:输入只有一行,包含一个正整数n.输出格式:输出只 ...

  4. JavaScript实现本地数据简单存取以及Json数据存取

    1.判断本地存储是否可用: if(window.localStorage) { // localStorge可用 }else { // localStorge不可用 } 2.存储数据: // 获取本地 ...

  5. python脚本文件删除

    昨天有需求需要用python脚本删除一个目录下的文件.遇到了点麻烦. 使用的是shutil.rmtree(dir)函数,这个函数可以删除有内容的目录,而shutil.rmdir(dir)只能删除空目录 ...

  6. subnetting and the subnet mask

    原文:https://www.techopedia.com/6/28587/internet/8-steps-to-understanding-ip-subnetting/5 Step 4 - Sub ...

  7. webservice简介及客户端搭建

    在环境变量中,CLASSPATH添加D:\apache-cxf-2.4.2\lib 新建CXF_HOME   D:\apache-cxf-2.4.2 在PATH中添加   D:\apache-cxf- ...

  8. [原]创建三个输入文本框,当光标离开文本框的时候如果文本框为空,则将文本框背景色设置为红色,如果不为空则为白色。提示:焦点进入控件的事件是onfocus,焦点离开控件的事件是onblur

    window.onload = function () {             var txts = document.getElementsByTagName('input');         ...

  9. Kmeans聚类算法

    K-means也是聚类算法中最简单的一种了,但是里面包含的思想却是不一般.最早我使用并实现这个算法是在学习韩爷爷那本数据挖掘的书中,那本书比较注重应用.看了Andrew Ng的这个讲义后才有些明白K- ...

  10. c语言-何为编程?

    大牛,请绕过. 新手,如果你怕我误人子弟,那也请绕过. 以下纯属个人YY 何为编程?何为程序? 说简单也简单,说复杂也复杂. 我在自学的道路上也有两三年了,也探索了两三年(非连续性),却只停留在入门阶 ...