现在的位置: 首页 > 综合 > 正文

hdu 1548 A strange lift

2018年04月26日 ⁄ 综合 ⁄ 共 2198字 ⁄ 字号 评论关闭

用bfs可以,个人觉得dfs应该很麻烦!觉得用最短路径更好理解,也容易实现,对点做预处理,用队列把有关点之间的权值设置成1,这样再用最短路径求解

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 is on floor A,and you want to go to floor B,how many times at least he havt 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
#include<iostream>
#include<queue>
using namespace std;
#define data 0x3f3f3f3f
int map[201][201],dis[201],mark[201],vis[201];
int x,y;
int main()
{
    int n,a[201],i,j;
    while(scanf("%d",&n)&&n)
    {
        scanf("%d%d",&x,&y);
        memset(mark,0,sizeof(mark));
        memset(map,data,sizeof(map));
        memset(dis,data,sizeof(dis));
        memset(vis,0,sizeof(vis));
        dis[x]=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        //队列预处理,将权值设为1,因为点与点之间可能形成环,所以要用一个数组标记,入队的点就不用再入队,只用记录权值就可以
        queue<int>q;
        q.push(x);
        while(!q.empty())
        {
            int first=q.front();
            q.pop();
            vis[first]=1;
            if(first+a[first]<=n)
            {
                map[first][first+a[first]]=1;
                if(!vis[first+a[first]])
                q.push(first+a[first]);
            }
            if(first-a[first]>=1)
            {
                map[first][first-a[first]]=1;
                if(!vis[first-a[first]])
                q.push(first-a[first]);
            }
        }
//dijkstra算法
        for(i=1;i<=n;i++)
        {
            int t,M=data;
            for(j=1;j<=n;j++)
            {
                if(!mark[j]&&dis[j]<M)
                {
                    M=dis[j];t=j;
                }
            }
            mark[t]=1;
            if(M==data) break;
            for(j=1;j<=n;j++)
            {
                dis[j]=dis[j]<dis[t]+map[t][j]?dis[j]:dis[t]+map[t][j];
            }
        }
        if(dis[y]<data)
        printf("%d\n",dis[y]);
        else
        printf("-1\n");
    }
    return 0;
}

 

抱歉!评论已关闭.