A
来源:互联网 发布:lcd1602只亮不显示数据 编辑:程序博客网 时间:2024/05/16 18:50
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 Sample Output
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"?
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.
5 1 53 3 1 2 50
3
第一次写博客,发个最短路的水题,Floyd算法稍微一优化就能水过
#include<stdio.h>#include<algorithm>#include<iostream>#include<string.h>#include<math.h>#include<string>#include<queue>using namespace std;#define N 255#define INF 0xfffffff#define ll __int64int map[N][N];int n,m;void init(){ for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) map[i][j]=INF; map[i][i]=0; }}void Floyd(){ for (int k=1;k<=n;k++) { for (int i=1;i<=n;i++) { if(map[i][k]!=INF) for (int j=1;j<=n;j++) map[i][j]=min(map[i][j],map[i][k]+map[k][j]); } }}int main(){ int s,t,i,j; int a[500]; while(scanf("%d",&n),n) { scanf("%d%d",&s,&t); init(); for(i=1;i<=n;i++) { scanf("%d",&a[i]); if(i+a[i]<=n) map[i][i+a[i]]=1; if(i-a[i]>=0) map[i][i-a[i]]=1; } Floyd(); if(map[s][t]==INF) printf("-1\n"); else printf("%d\n",map[s][t]); } return 0;}
0 0
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- A
- A*
- a
- A
- a
- 原始类型.Class 及 Class.forName(String class) 与 类名.class 的区别
- Linux下忘记MySQL的root密码的解决方法
- 二叉树怎样序列化才能重建
- JVM 学习笔记(三) 垃圾收集器与内存分配策略
- IAR编辑器字体配置
- A
- 扛得住mysql 性能优化
- 大话设计模式读书笔记(十四) 适配器模式
- Developing DataBase Applications Using MySQL Connector/C++ 中文文本
- PreparedStatement中的execute、executeQuery和executeUpdate之间的区别
- Java俄罗斯轮盘死亡游戏
- gdb调试器用法
- Python scikit-learn包 决策树和随机森林实例代码
- hibernate教程--持久化类状态详解