天天看點

HDU 1548:A strange lift A strange lift

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 5
3 3 1 2 5
0
        

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 202

using 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 202

using 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 202

using 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;
}