hdu1548 A strange lift
来源:互联网 发布:新濠天地信誉第一js 编辑:程序博客网 时间:2024/06/04 17:48
题目链接:
点击打开链接
题目描述:
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17968 Accepted Submission(s): 6706
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"?
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.
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 53 3 1 2 50
Sample Output
3大致题意:有一个n层的电梯,每层有一个数字k[i],表示可以上升k[i]层,或者下降k[i]层。但是不能下降到小于1层,或者大于n层。给出两层a,b,问从第a层到第b层最少需要按多少次按钮?分析:把每层看成有向图的顶点,如果从第i层可以一次到达第j层,则连一条从顶点i到顶点j的有向边,并且长度为1。转化为求有向图中顶点a到顶点b的最短路径长度。看到n最大为200,我先是使用了Floyd算法,然后TLE,后来使用Dijkstra算法。超时代码:#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>using namespace std;const int N=300;int n,a,b,k[N],g[N][N];int main(){ while(~scanf("%d",&n)) { if(n==0)break; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) g[i][j]=10000; cin>>a>>b; for(int i=1;i<=n;i++) { scanf("%d",&k[i]); if(i-k[i]>=1) g[i][i-k[i]]=1; if(i+k[i]<=n) g[i][i+k[i]]=1; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) for(int d=1;d<=n;d++) g[i][j]=min(g[i][j],g[i][d]+g[d][j]); cout<<g[a][b]<<endl; } return 0;}AC代码:#include<iostream>#include<stdio.h>#include<string.h>#include<cmath>using namespace std;const int N=300;const int MAX=100000000;int n,a,b,k[N],g[N][N];int vis[N],dist[N];void dijkstra(){ memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) dist[i]=g[a][i]; dist[a]=0; vis[a]=1; for(int i=1;i<=n;i++) { int mark=-1,mindis=MAX; for(int j=1;j<=n;j++) if(!vis[j]&&dist[j]<mindis) { mark=j; mindis=dist[j]; } if(mark==-1) return; vis[mark]=1; for(int j=1;j<=n;j++) if(!vis[j]) dist[j]=min(dist[j],dist[mark]+g[mark][j]); }}int main(){ while(~scanf("%d",&n)) { if(n==0)break; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) g[i][j]=MAX; cin>>a>>b; for(int i=1;i<=n;i++) { scanf("%d",&k[i]); if(i-k[i]>=1) g[i][i-k[i]]=1; if(i+k[i]<=n) g[i][i+k[i]]=1; } dijkstra(); if(dist[b]==MAX)printf("-1\n"); else printf("%d\n",dist[b]); } return 0;}
0 0
- hdu1548 A strange lift
- HDU1548 A strange lift
- HDU1548 A strange lift
- hdu1548 A strange lift
- HDU1548---A strange lift
- hdu1548 A strange lift
- hdu1548 a strange lift
- HDU1548-A strange lift
- Hdu1548 A strange lift
- HDU1548 A strange lift
- hdu1548 A strange lift
- HDU1548 A strange lift
- HDU1548-A strange lift
- hdu1548 [A strange lift] bfs
- HDU1548:A strange lift(BFS)
- HDU1548:A strange lift( BFS)
- HDU1548:A strange lift(Dijkstra)
- hdu1548 A strange lift(bfs)
- 基于S3C6410的ARM11学习(四) 核心初始化之设置处理器模式
- IOS Xib使用——Xib表示局部界面
- iOS上应用如何兼容32位系统和64位系统
- CSS实现背景透明,文字不透明,兼容所有浏览器
- 无法打开源文件“stdafx.h”
- hdu1548 A strange lift
- 教你在 Debian 和 Ubuntu 上升级 MySQL
- 【宏芸老师】人生的十枚金币
- 企业级证书测试报错相关
- iOS开发 webView加载html数据
- 函数
- 内核程序实现多文件的调用
- SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"
- Go如何实现枚举小实例分享