A strange lift

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

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 1142 1372 1072 1690 
 
最短路的模板题,代码还是很好理解的。
 

题意:一个特别的电梯,按up可升上k[i]层,到大i+k[i]层,down则到达i-k[i]层,最高不能超过n,最低不能小于1,给你一个起点和终点,问最少可以按几次到达目的地。在一个N层高的楼有一个奇怪的电梯,在每一层只能上升或下降一个特定的层数,中间不会停止,在给定的条件下,问能不能到达指定楼层,可以到达的话返回转操作次数,不可以的话返回-1.

附上代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#define M 205
#define MAX 0x3f3f3f3f
using namespace std;
int map[M][M],vis[M],dis[M];
int main()
{
int n,a,b,i,j,s;
while(~scanf("%d",&n)&&n)
{
scanf("%d%d",&a,&b);
memset(vis,,sizeof(vis));
memset(dis,,sizeof(dis));
for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
if(i==j)
map[i][j]=; //同一个地方距离为0
else
map[i][j]=MAX;
}
for(i=; i<=n; i++)
{
scanf("%d",&s);
if(i+s<=n) //标记这一层电梯可以去的楼层
map[i][s+i]=;
if(i-s>=)
map[i][i-s]=;
}
vis[a]=; //起点已走过
for(i=; i<=n; i++)
dis[i]=map[a][i]; //初始化距离为每个点到起点的距离
int min,k,t;
for(i=; i<=n; i++)
{
min=MAX;
for(j=; j<=n; j++)
if(!vis[j]&&dis[j]<min) //每次都找离终点最近的点
{
min=dis[j];
t=j;
}
vis[t]=; //标记为已经找过此点
for(j=; j<=n; j++)
if(!vis[j]&&map[t][j]<MAX) //从最近的点到下一个点的距离与初始距离进行比较
if(dis[j]>dis[t]+map[t][j])
dis[j]=dis[t]+map[t][j];
}
if(dis[b]<MAX)
printf("%d\n",dis[b]);
else //不能到 则输出-1
printf("-1\n");
}
return ;
}

邻接表代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
struct Edge
{
int from,to,val,next;
}edge[];
int tol,s,t,n;
int dis[];
bool vis[];
int head[]; void init()
{
tol=;
memset(head,-,sizeof(head));
} void addEdge(int u,int v)
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].val=;
edge[tol].next=head[u];
head[u]=tol++;
} void getmap()
{
int x;
for(int i=;i<=n;i++)
{
scanf("%d",&x);
if(i-x>=) addEdge(i,i-x);
if(i+x<=n) addEdge(i,i+x);
}
memset(vis,false,sizeof(vis));
memset(dis,inf,sizeof(dis));
} void spfa()
{
queue<int>q;
q.push(s);
vis[s]=true;
dis[s]=;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].val)
{
dis[v]=dis[u]+edge[i].val;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
if(dis[t]<inf)
printf("%d\n",dis[t]);
else
printf("-1\n");
return;
} int main()
{
int i,j;
while(~scanf("%d",&n)&&n)
{
scanf("%d%d",&s,&t);
init();
getmap();
spfa();
}
}

最新文章

  1. 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(四)地图导航控件模块
  2. iPad开发
  3. 安装配置php5.4 win2003
  4. Oracle EBS R12 电子技术参考手册 - eTRM (电子文档)
  5. Bootstrap3.0学习第九轮(CSS补充)
  6. 20145220&amp;20145209&amp;20145309信息安全系统设计基础实验报告(1)
  7. rest api设计[资源]
  8. 使用servlet实现文件上传
  9. Poco之ftp获取文件列表以及下载文件
  10. iOS 数据持久化
  11. DatabaseMetaData的用法(转)
  12. shell学习之常用命令总结
  13. pragma message任务
  14. testNG实现test失败后重复执行,
  15. Nginx支持Socket转发过程详解
  16. ABP项目启动及源代码结构
  17. python变量存储
  18. Ubuntu16.04安装Zabbix3.2(快速安装教程)
  19. Selenium 动作链
  20. [转载]oracle游标概念讲解

热门文章

  1. Leetcode55. Jump Game跳跃游戏
  2. 【JZOJ5060】【GDOI2017第二轮模拟day1】公路建设 线段树+最小生成树
  3. 2019-8-31-C#-对-byte-数组进行模式搜索
  4. (续)使用Django搭建一个完整的项目(Centos7+Nginx)
  5. OSGi教程:Resource API Specification
  6. spark-ML之朴素贝叶斯
  7. day39-Spring 13-Spring的JDBC模板:默认连接池的配置
  8. More Effective C++: 01基础议题
  9. poj1637&amp;&amp;hdu1956 混合欧拉回图判断
  10. switch范围判断