HDU1548 A strange lift 奇怪的电梯

题目描述:

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

题意:

电梯在每一层都有一个数k,只能往上k层或者往下k层。让你求从m层到n层至少需要按多少次电梯按钮。
分析:
本题的话我们将其看成一个最短路问题。将层数看做点,能够互相到达的层看做路径。
路径长度置为1。这样的话,按几次就直接知道了。

#include"stdio.h"
#include"string.h"
#include"algorithm"
   using namespace std;
#define INF 10000000
void ShortestPath_dijkstra(int Graph[][201],int A,int B,int N)
{
    int vis[201],dis[201],p[201];
    int i,j,k;
    for(int i=1;i<=N;i++)
    {
        vis[i]=1;
        dis[i]=Graph[A][i];
        p[i]=A;
    }
    vis[A]=0;
    for(i=1;i<N;i++)
    {
        int minx=INF;
        j=0;k=0;
        while(j<N)
        {
            if(vis[j]&&minx>dis[j])
            {
                minx=dis[j];
                k=j;
            }
            j++;
        }
        vis[k]=0;
        for(j=1;j<=N;j++)
        {
            if(vis[j]&&minx+Graph[k][j]<dis[j])
            {
                dis[j]=Graph[k][j]+minx;
                p[j]=k;
            }
        }
    }
    if(dis[B]==INF)
        printf("%d\n",-1);
    else
    {
        printf("%d\n",dis[B]);
    }
}
int main()
{
    int N,A,B;
    int Graph[201][201];
    while(~scanf("%d",&N))
    {
        if(N==0)
            break;
        scanf("%d%d",&A,&B);
        for(int i=0;i<=N;i++)
            for(int j=0;j<=N;j++)
                if(i==j)
                    Graph[i][j]=0;
                else
                    Graph[i][j]=INF;
        for(int i=1;i<=N;i++)
        {
            int a;
            scanf("%d",&a);
            if(i-a>=1)
                Graph[i][i-a]=1;
            if(i+a<=N)
                Graph[i][i+a]=1;
        }
      ShortestPath_dijkstra(Graph,A,B,N);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43506138/article/details/87560180
今日推荐