Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13724    Accepted Submission(s): 5239

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 are
on floor A,and you want to go to floor B,how many times at least he has
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
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int N,A,B;
int flor[250];
int dir[]={-1,1};//上下方向
struct node{
int x;//楼层
int step_cnt;//步数
};
node now,nextf;
node vs,vd;//起点和终点
int leap;
int vis[250];//标记数组只需一维,即标记刚入队的nextf,下次不用再回到此处,
//因你到了nextf结点后,只会有上下两个选择,即使下次再回到此处
//面对的仍是上下两个选择,而在第一次就可抉择
void in_put()
{
scanf("%d%d",&A,&B);
for(int i=1;i<=N;++i)
scanf("%d",&flor[i]);
vs.x=A;
vd.x=B;
}
void bfs()
{
queue<node>Q;
vs.step_cnt=0;
Q.push(vs);
while(!Q.empty()){
now=Q.front();
Q.pop();
if(now.x==vd.x) {leap=1;return;}
for(int i=0;i<2;++i){
nextf.x=now.x+dir[i]*flor[now.x];
nextf.step_cnt=now.step_cnt+1;
if(nextf.x>=1&&nextf.x<=N&&!vis[nextf.x]) {Q.push(nextf);vis[nextf.x]=1;}//迷之wa,原因在于写出了vis[now.x]=1;
}
}
}
int main()
{
while(scanf("%d",&N)&&N){
leap=0;
memset(vis,0,sizeof(vis));
in_put();
bfs();
if(leap)
printf("%d\n",now.step_cnt);
else printf("-1\n");
}
}

A strange lift的更多相关文章

  1. HDU 1548 A strange lift (最短路/Dijkstra)

    题目链接: 传送门 A strange lift Time Limit: 1000MS     Memory Limit: 32768 K Description There is a strange ...

  2. 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 ...

  3. bfs A strange lift

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

  4. hdu 1548 A strange lift

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...

  5. 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 ...

  6. HDU 1548 A strange lift (Dijkstra)

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

  7. HDU1548——A strange lift(最短路径:dijkstra算法)

    A strange lift DescriptionThere is a strange lift.The lift can stop can at every floor as you want, ...

  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 (bfs)

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

随机推荐

  1. Todd's Matlab讲义第2讲:Matlab 编程

    Matlab也可以编程,可存为以.m为后缀的文件,称为M文件.M文件有两种:函数和脚本. 函数程序 点击新建图标,在打开的窗口里输入如下内容: function y = myfunc (x) y = ...

  2. 关于Eclipse部署openfire3.8.2源码的体会

    因为公司要做人际银行的一个项目需要openfire(服务器)+asmack(客户端),所以需要对消息的推送及消息发送知识的积累.所以需要研究xmpp,以前不是很了解这个技术,现在需要学习.首先就得部署 ...

  3. SSH 内网端口转发实战

    导读 大家都知道SSH是一种安全的传输协议,用在连接服务器上比较多.不过其实除了这个功能,它的隧道转发功能更是吸引人. 如果两个内网之间的linux服务器需要互相登录,或需要互相访问内网某个端口,担忧 ...

  4. 从零开始写一个武侠冒险游戏-7-用GPU提升性能(2)

    从零开始写一个武侠冒险游戏-7-用GPU提升性能(2) ----把地图处理放在GPU上 作者:FreeBlues 修订记录 2016.06.21 初稿完成. 2016.08.06 增加对 XCode ...

  5. MySql 插入数据中文乱码

    在数据库连接URL后加上characterEncoding=UTF-8 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/ssm ...

  6. Android自定义dialogdemo

    很多时候,我们需要自己去定义dialog,目前我们就遇见了这样一个需求,我的想法是自己定义一个dialog,如果有list的话就使用listview,如果有msg的话就使用msg,并且取消和确定按钮也 ...

  7. 【OpenStack】OpenStack系列13之Nova源码解析与API扩展

    学习思路 议程:代码结构-主干流程-分层架构-业务模型-数据库模型-消息模型 分布式架构:Api:横向扩展    rpc:纵向扩展 分层架构:Controller接口层.View/Manager逻辑层 ...

  8. static_cast dynamic_cast const_cast reinterpret_cast总结对比

    [本文链接] http://www.cnblogs.com/hellogiser/p/static_cast-dynamic_cast-const_cast-reinterpret_cast.html ...

  9. Java for LeetCode 053 Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  10. XX管理系统案例

    一.登录界面建立登录文件夹Login,在此目录下面建立如下文件:Index.htm:登录页面ValidateCode.cs:生成验证码ProcessVerification.ashx:处理验证码Com ...