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. 丢掉鼠标-Mac神软Alfred使用手册

    上篇: http://wellsnake.com/jekyll/update/2014/06/15/001/?utm_source=tuicool 下篇: http://wellsnake.com/j ...

  2. 优化SQLServer数据库加快查询速度

    查询速度慢的原因很多,常见如下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列导致查询不优化. 4.内存不足 ...

  3. javascript焦点图自动播放

    这次是完整版,网页点开就能自动播放 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  4. 【codevs】1860 最大数

    1860 最大数     题目描述 Description 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 输入描述 Input Description 第一行一个正整数n. ...

  5. md5校验问题

    描述: 最近跟同事做数据对接,接收完数据,有个md5校验,发现总是对不上 首先把解密之后的明文直接写成变量在md5的工具类main函数执行,发现居然对上了, 然后测试环境debug接收的文件,md5加 ...

  6. NDK常见问题

    1. 忽略编译警告为错误 APP_CPPFLAGS += -Wno-error=format-security 2. android studio 手动编译 __android_log_print 错 ...

  7. JQuery 多选按钮checkbox

    JQuery 多选按钮checkbox 在需要全选和选择部分的时候我们就需要多选在这里主要介绍了具体的实现 JQuery $(function () { //全选或全不选 $("#allbo ...

  8. zencart 自定义函数

    ---------------------------------------------------------------------------------------------------- ...

  9. Chapter 2 Open Book——6

    Last night I'd discovered that Charlie couldn't cook much besides friedeggs and bacon. 昨天晚上我终于发现查理除了 ...

  10. apache 添加到windows服务

    cmd命令行下 输入 “d:\apache\bin\httpd.exe” -k install 如果是resin的话直接运行目录下的setup就可以了, 前提是需要.net framework 3.5