HDU 1548:A strange lift

来源:互联网 发布:日语翻译哪个软件好 编辑:程序博客网 时间:2024/04/28 02:04

A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24740    Accepted Submission(s): 8934

点击打开题目链接
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 53 3 1 2 50
 

Sample Output
3 
3种方法,第一种:Dijkstra求最短路径;第二种方法:普通BFS广搜一波;第三种方法:dp做法。
方法1:最短路做法,题目会卡时间,Flody做法超时,Dijkstra可以AC。注意道路是单向的。
#include<iostream>#include<cstdio>#include<queue>#include<vector>#include<cstring>#include<string>#include<stack>#include<map>#include<set>#define INF 0x3f3f3f3f#define MAXN 202using namespace std;int Map[MAXN][MAXN];int N,A,B;void InitMap(){    for(int i = 1; i <= N; i++)    {        Map[i][i] = 0;        for(int j = i+1; j <= N; j++)            Map[i][j] = Map[j][i] = INF;    }}int dis[MAXN];int vis[MAXN];void Dijkstra(int start){    int mindis,u;    for(int i = 1; i <= N; i++)    {        vis[i] = 0;        dis[i] = Map[start][i];    }    vis[start] = 1;    for(int i = 1; i <= N; i++)    {        mindis = INF;        u = 0;        for(int j = 1; j <= N; j++)        {            if(vis[j]==0 && dis[j]<mindis)            {                mindis = dis[j];                u = j;            }        }        if(u == 0) break;        vis[u] = 1;        for(int j = 1; j <= N; j++)        {            if(vis[j]==0)            {                if(dis[u]+Map[u][j]<dis[j])                    dis[j] = dis[u]+Map[u][j];            }        }    }}int main(){    int temp;    while(~scanf("%d",&N))    {        if(N == 0) break;        scanf("%d%d",&A,&B);        InitMap();        for(int i = 1; i <= N; i++)        {            scanf("%d",&temp);            if(i-temp>=1)                Map[i][i-temp] = 1;            if(i+temp<=N)                Map[i][i+temp] = 1;        }        Dijkstra(A);        if(dis[B]<INF)            printf("%d\n",dis[B]);        else            printf("-1\n");    }    return 0;}
方法2:广搜,和Catch That Cow题目方法一样。
#include<iostream>#include<cstdio>#include<queue>#include<vector>#include<cstring>#include<string>#include<stack>#include<map>#include<set>#define INF 0x3f3f3f3f#define MAXN 202using namespace std;int N,Start,End;int a[MAXN];int vis[MAXN];struct pos{    int level;    int steps;};void bfs(){    pos cur,nex;    cur.level = Start;    cur.steps = 0;    queue<pos>qu;    qu.push(cur);    vis[Start] = 1;    while(!qu.empty())    {        cur = qu.front();        qu.pop();        if(cur.level == End)        {            printf("%d\n",cur.steps);            return;        }        nex.level = cur.level + a[cur.level];        nex.steps = cur.steps + 1;        if(nex.level <= N)        {            if(vis[nex.level] == 0)            {                vis[nex.level] = 1;                qu.push(nex);            }        }        nex.level = cur.level - a[cur.level];        nex.steps = cur.steps + 1;        if(nex.level >= 1)        {            if(vis[nex.level] == 0)            {                vis[nex.level] = 1;                qu.push(nex);            }        }    }    printf("-1\n");    return;}int main(){    while(~scanf("%d",&N))    {        if(N == 0) break;        scanf("%d%d",&Start,&End);        for(int i = 1; i <= N; i++)        {            scanf("%d",&a[i]);            vis[i] = 0;        }        bfs();    }    return 0;}

方法3:dp做法。
#include<iostream>#include<cstdio>#include<queue>#include<vector>#include<cstring>#include<string>#include<stack>#include<map>#include<set>#define INF 0x3f3f3f3f#define MAXN 202using namespace std;int N,Start,End;int dp[MAXN];   ///dp[i]存放从Start层到i层按按钮的最少次数int a[MAXN];int main(){    while(~scanf("%d",&N))    {        if(N == 0) break;        scanf("%d%d",&Start,&End);        for(int i = 1; i <= N; i++)        {            scanf("%d",&a[i]);            dp[i] = INF;               }        dp[Start] = 0;         int flag = 1;  ///初始为1,为了进入循环        while(flag)        {            flag = 0;  ///先初始化为0            for(int i = 1; i <= N; i++)              {                if(i-a[i]>=1)                {                    if(dp[i-a[i]]>dp[i]+1)  ///如果有数据更新,flag为1,可以继续循环                    {                        dp[i-a[i]] = dp[i]+1;                        flag = 1;                    }                }                if(i+a[i]<=N)                {                    if(dp[i+a[i]]>dp[i]+1)                    {                        dp[i+a[i]] = dp[i]+1;                        flag = 1;                    }                }            }            ///如果flag没有更新,则说明所有情况都考虑过了。        }        if(dp[End]<INF)            printf("%d\n",dp[End]);        else            printf("-1\n");    }    return 0;}


0 0