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 5 3 3 1 2 5 0
题目意思:给出n层电梯,起始位置和末尾位置,要求选出这n步里面最少的操作组合在一起,使得从起始位置到末位位置的操作次数最少~~~
这里用到了最短路Dijkstra~最少的操作次数为:1 + 3 --------4 -----------4 - 1 ----------3 -----------------3 + 2------------5 所以就是3
#include <stdio.h>
#include <math.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#define setbit(x,y) x|=(1<<(y)) //将X的第Y位置1
#define clrbit(x,y) x&=~(1<<(y)) //将X的第Y位清0
#define sf scanf
#define pf printf
#define INF 1 << 29
#define eps 1e-6
const double PI = acos(-1.0);
#define lint __int64
#define LL long long
#define MAXN 1e9 + 7
#define maxn 10005
//101^110=011 异或
#define ULLint unsigned long long //2^64-1>1.8*10^19
#define clr(x) memset(x, 0, sizeof(x))
#define Clr(x) memset(x, -1, sizeof(x))
using namespace std;
#define N 210
int dis[N], map[N][N];
bool visit[N];
int n;
void Dijkstra(int start) {
int Min, k = 0;
memset(visit, 0, sizeof(visit));
for(int i = 1; i <= n; ++i)
dis[i] = map[start][i];
dis[start] = 0;
visit[start] = 1;
for(int i = 1; i <= n; ++i) {
Min = INF;
for(int j = 1; j <= n; ++j)
if(!visit[j] && Min > dis[j]) {
Min = dis[k];
k = j;
}
if(Min == INF) break;
visit[k] = 1;
for(int j = 1; j <= n; ++j)
if(!visit[j] && dis[j] > dis[k] + map[k][j])
dis[j] = dis[k] + map[k][j];
}
}
int main() {
int u, v;
int tt;
while(scanf("%d", &n) && n) {
memset(dis, 0, sizeof(dis));
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
map[i][j] = INF;
scanf("%d%d", &v, &u);
for(int i = 1; i <= n; ++i) {
scanf("%d", &tt);
if(i + tt <= n)
map[i][i + tt] = 1;
if(i - tt >= 1)
map[i][i - tt] = 1;
}
Dijkstra(v);
if(dis[u] == INF) printf("-1\n");
else printf("%d\n", dis[u]);
}
return 0;
}