A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 15570    Accepted Submission(s): 5832

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
 
Recommend
8600   |   We have carefully selected several similar problems for you:  1385 1242 

pid=1142" style="color:rgb(26,92,200); text-decoration:none">1142 1217 1253 

 

Statistic | 

pid=1548" style="color:rgb(26,92,200); text-decoration:none">Submit | 

problemid=1548" style="color:rgb(26,92,200); text-decoration:none">Discuss | 

pid=1548" style="color:rgb(26,92,200); text-decoration:none">Note

电梯仅仅有两个方向,向上或者向下。既然是求最短路径。也就用到dijkstra算法(无负权值)。

仅仅要能想到怎样构造算法即可。假设自己的算法,却不知道怎样用来解题,也都是没用的。

在这里我想给大家说一下。

在以后做题的过程中,不要仅仅看别人的代码,要看思想,别人为什么这样写。然后依据自己想象的思想写一遍代码。写的过程中不要

看别人的代码。即使不正确也无所谓,这样印象最深,以后也就随手敲来、

详细还是代码里面见:

#include <stdio.h>
#include<string.h>
#include <queue>
using namespace std;
struct node
{
int pos,t;
friend bool operator<(node a,node b)
{
return a.t>b.t;
}
};
priority_queue<node>s;
int lift[205],vis[205],n;
int dijkstra(int st,int ed)
{
node temp,temp1;
int flag=0;
temp.pos=st,temp.t=0;
s.push(temp);
while(!s.empty())
{
temp1=temp=s.top(),s.pop();
vis[temp.pos]=1;
if(temp.pos==ed)
{
flag=1;
break;
}
temp.pos=temp1.pos-lift[temp1.pos];//
temp.t=temp1.t+1;
if(temp.pos>=1&&temp.pos<=n&&!vis[temp.pos])
s.push(temp);
temp.pos=temp1.pos+lift[temp1.pos];
temp.t=temp1.t+1;
if(temp.pos>=1&&temp.pos<=n&&!vis[temp.pos])
s.push(temp);//和以往的代码不同的也就这个地方。曾经做的要么是个矩阵,要么是个树,如今就两个方向了。。推断电梯的
这两个方向,进队列即可了
}
if(flag)
return temp.t;
else
return -1;
}
int main()
{
int st,ed;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
scanf("%d %d",&st,&ed);
for(int i=1;i<=n;i++)
scanf("%d",&lift[i]);
memset(vis,0,sizeof(vis));
while(!s.empty())
s.pop();
printf("%d\n",dijkstra(st,ed));
}
return 0;
}

hdu1584 A strange lift (电梯最短路径问题)的更多相关文章

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

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

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

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

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

  4. hdu 1548 A strange lift (bfs)

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

  5. A strange lift

    Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...

  6. A strange lift HDU - 1548

    There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 ...

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

  8. Hdu1548 A strange lift 2017-01-17 10:34 35人阅读 评论(0) 收藏

    A strange lift Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tota ...

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

随机推荐

  1. POJ 2315:Football Game(博弈论)

    [题目链接] http://poj.org/problem?id=2315 [题目大意] 两名球员轮流从N个球中挑出不多于M个射门,每个球半径都是R,离球门S. 每次只能踢出L以内的距离.进最后一个球 ...

  2. 静态html分页

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. iOS面试_1.浅析内存管理

    为了开学的面试,就在博客里总结一下面试会问到的问题,今天就来谈谈内存管理,看到一篇文章非常不错,http://vinceyuan.cnblogs.com/,深入浅出,推荐大家去看看! Objectiv ...

  4. 【jQuery】jquery中 使用$('#parentUid').attr(parentUid);报错jquery-1.11.3.min.js:5 Uncaught TypeError: Cannot read property 'nodeType' of undefined

    jquery中 使用$('#parentUid').attr(parentUid);报错jquery-1.11.3.min.js:5 Uncaught TypeError: Cannot read p ...

  5. 所见即所得的网页设计工具 Macaw

    所见即所得的网页设计工具 Macaw: 2014年最具前景的12款创新产品 1\   http://macaw.co/ 2\   http://www.kickstarter.com/projects ...

  6. jquery获取select下拉框的前一个,后一个,第一个,最后一个option对象

    $("select option:selected").next(); <select> <option value="1" selected ...

  7. [Android Traffic] 看无线电波如何影响网络操作]

    转载自: http://blog.csdn.net/kesenhoo/article/details/7391031 Optimizing Downloads for Efficient Networ ...

  8. CAP 定理的含义

    分布式系统(distributed system)正变得越来越重要,大型网站几乎都是分布式的. 分布式系统的最大难点,就是各个节点的状态如何同步.CAP 定理是这方面的基本定理,也是理解分布式系统的起 ...

  9. javascript数组及操作方法

    数组及操作方法 数组就是一组数据的集合,javascript中,数组里面的数据可以是不同类型的. 定义数组的方法 //对象的实例创建 var aList = new Array(1,2,3); //直 ...

  10. Python 自用代码(某方标准类网页源代码清洗)

    用于mongodb中“标准”数据的清洗,数据为网页源代码,须从中提取: 标准名称,标准外文名称,标准编号,发布单位,发布日期,状态,实施日期,开本页数,采用关系,中图分类号,中国标准分类号,国际标准分 ...