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. Entity Framework 系统约定配置

    前言 Code First之所以能够让开发人员以一种更加高效.灵活的方式进行数据操作有一个重要的原因在于它的约定配置.现在软件开发越来越复杂,大家都试图将软件设计的越来越灵活,很多内容我们都希望是可配 ...

  2. 在spring中获取代理对象代理的目标对象工具类

    昨天晚上一哥们需要获取代理对象的目标对象,查找了文档发现没有相应的工具类,因此自己写了一个分享给大家.能获取JDK动态代理/CGLIB代理对象代理的目标对象. 问题描述:: 我现在遇到个棘手的问题,要 ...

  3. HNU 12847 Dwarf Tower(最短路+队列优化)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12847 解题报告:有n样物品,编号从1到n第i样物品可以通过金 ...

  4. 他们在军训,我在搞 OI(一)

    Day 1 理论上,我现在不应该坐在电脑前打字,因为早在今天上午 6:20 全体新高一同学就坐车前往军(无)训(尽)基(炼)地(狱)了,而今天上午 6:20 我还在被窝里呢…… 没错,我旷掉了军训,然 ...

  5. arch linux 新版安装(转)

    原文地址:http://blog.sina.com.cn/s/blog_69e5d8400101bqlj.html 正式安装: 以下,是安装的具体步骤,我将做尽可能详细的解说: 一.基本系统的安装 进 ...

  6. puppet之自定义fact(转载)

    1.使用环境变量'FACTERLIB'创建fact 1.1.在自定义目录里面定义一个fact,列出当前系统登录的用户数 [root@agent1 ~]# vim /var/lib/puppet/kis ...

  7. 响应式Web设计(Responsive Web design)

    中文名 响应式Web设计 提出时间 2010年5月 英    文 Responsive Web design 解    释 一个网站能够兼容多个终端 目    的 解决移动互联网的浏览 优    点 ...

  8. android获取手机信息大全

    IMEI号,IESI号,手机型号: private void getInfo() { TelephonyManager mTm = (TelephonyManager) getSystemServic ...

  9. 【Python】Django支持事务方式

    代码: with transaction.atomic(): for i in xrange(int(svc_instance_num)): tmp_fileprotect_svc_instance ...

  10. css样式自适应,支持数字

    td加上style="word-break: break-all;word-wrap: break-word;"样式即可