hdu 1548 A strange lift

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  1242  1142  1217  1253 
 

典型的最短路问题,但是是属于变形了的Dijkstra算法,一开始交了5次都是WA,不知道什么原因,感觉写的很对,后来看了一下大神的代码发现是把a,b写成了局部变量,不知为什么一定要写成全局变量......
题意:电梯在每一层都有一个数k,只能往上k层或者往下k层。让你求从a层到b层至少需要嗯多少次电梯按钮。
思路:最短路的变行问题,将层数看做点,能够互相到达的层看做路径。
这也是最短路径的问题,与以往的最短路径有所不同,这题首先要做的是把在某一层向上向下能一步到达的层数用数组对应起来,然后就是dijkstra算法求最短路径了,看看参考程序:
#include <iostream>
#include <cstdio>
using namespace std;

const int INF=0x3f3f3f3f;
int map[205][205];
int dist[205];
bool vis[205];
int n,a,b;
void dijkstra(int start,int end)
{
    for(int i=1; i<=n; i++)
    {
        dist[i]=map[start][i];
        vis[i]=false;
    }
    dist[start]=0;
    vis[start]=true;
    for(int i=2; i<=n; i++)
    {
        int min=INF;
        int post;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j]&&dist[j]<min)
            {
                min=dist[j];
                post=j;
            }
        }
        if(min==INF)
            return;
        vis[post]=true;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j]&&dist[j]>dist[post]+map[post][j])
            {
                dist[j]=dist[post]+map[post][j];
            }
        }
    }
}
int main()
{
    int k[205];
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        //cin>>a>>b;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                map[i][j]=INF;
        scanf("%d%d",&a,&b);
        for(int i=1; i<=n; i++)
        {
            //cin>>c;
            scanf("%d",&k[i]);
            if(i-k[i]>=1)
                map[i][i-k[i]]=1;
            if(i+k[i]<=n)
                map[i][i+k[i]]=1;
        }
        dijkstra(a,b);
        if(dist[b]!=INF)
            //cout<<dist[b]<<endl;
            printf("%d\n",dist[b]);
        else
            //cout<<-1<<endl;
            printf("-1\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值