HDU-1548--A strange lift--(BFS,剪枝)
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 |
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,剪枝)的更多相关文章
- hdu 1548 A strange lift (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
- 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 ...
- 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 ...
- 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 ...
- HDU 1548 A strange lift(最短路&&bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1548 A strange lift (Dijkstra)
A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...
- HDU 1548 A strange lift 搜索
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 1548 A strange lift (广搜)
题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...
- HDU 1548 A strange lift 题解
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
随机推荐
- 判定生死的心跳机制 --ESFramework 4.0 快速上手(07)
在Internet上采用TCP进行通信的系统,都会遇到一个令人头疼的问题,就是"掉线".而"TCP掉线"这个问题远比我们通常所能想象的要复杂的多 -- 网络拓扑 ...
- OMCS开发手册(03) -- 多媒体服务器
前面我们已经详细介绍了基于OMCS开发网络多媒体应用的客户端程序所必需掌握的内容,现在我们来看一下OMCS服务端的开发.对于使用者而言,OMCS的服务端就非常简单了,只要实现一个用户验证的接口,挂接到 ...
- cmd alias 自定义命令
简短步骤:1.关闭所有在运行的CMD窗口2.创建文件C:\cmd-alias.bat,包含以下内容:[python] view plain copydoskey sayhello=echo Hello ...
- Openjudge-计算概论(A)-简单算术表达式求值
描述: 两位正整数的简单算术运算(只考虑整数运算),算术运算为: +,加法运算:-,减法运算:*,乘法运算:/,整除运算:%,取余运算. 算术表达式的格式为(运算符前后可能有空格):运算数 运算符 运 ...
- android Tweened Animations
Android提供了两种类型的动画: 一类是Tween动画:提供了旋转.移动.伸展和淡出等效果: 第二类是Frame-by-frame动画:这一类Animations可以创建一个Drawable序列, ...
- 2015 Multi-University Training Contest 6
1001 Average 忍不住又补了一题. 只要枚举1与2之间1给2,2给1,什么都不做三种状态. 后面的情况都已经决定了. (估计只有我比赛的时候把a candy当成a个糖果了吧QAQ) # in ...
- php 分页类(1)
inter.php <head> <meta http-equiv="Content-Type" content="text/html; charset ...
- Class.forName和ClassLoader.loadClass的比较
文章出处:http://blog.csdn.net/wikijava/article/details/5576043 Class的装载分了三个阶段,loading,linking和initializi ...
- Emacs阅读chm文档
.title { text-align: center; margin-bottom: .2em } .subtitle { text-align: center; font-size: medium ...
- jQuery实现 图片的局部放大效果
<html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title> ...