【HDU 1548 --- A strange lift】BFS||最短路问题(floyd,dijkstra都可以)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_41879343/article/details/102686447

【HDU 1548 --- A strange lift】BFS||最短路问题(floyd,dijkstra都可以)


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

解题思路

细读题意,我们会发现此题实际上就是一个求最短路径的题,说白了就是找最短几次能从A到达B。
用BFS时,我们需要注意将每层区分开,这样才能知道到底是第几层了,第几步了。那么怎么区分呢?
只需我们添加完一层就添加进去一个-1,当下次遇见-1时则表示那一层已经遍历完了。

用floyd或者dijkstra时就更简单了,就是一个板子题,只需要记录次数就行。

AC代码1(BFS):

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 205;
int n,a,b,arr[MAXN];
int ans=0;
bool vis[MAXN];
queue<int> q;

void bfs()
{
    q.push(a);
    q.push(-1);
    vis[a]=true;
    while(!q.empty())
    {
        int front=q.front();
        if(front==-1)
        {
            if(q.size()==1)
            {
                q.pop();
                break;
            }
            q.pop();
            q.push(-1);
            ans++;
            continue;
        }
        q.pop();
        int x=front+arr[front];
        int y=front-arr[front];
        if(x==b || y==b) break;
        if(x<=n && !vis[x]) q.push(x),vis[x]=true;
        if(y>=1 && !vis[y]) q.push(y),vis[y]=true;
    }
}

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    while(cin >> n && n)
    {
        ans=0;
        cin >> a >> b;
        for(int i=1;i<=n;i++) cin >> arr[i],vis[i]=false;
        if(a==b)
        {
            cout << 0 << endl;
            continue;
        }
        bfs();
        if(!q.empty())
            cout << ans+1 << endl;
        else
            cout << -1 << endl;
        while(!q.empty()) q.pop();
    }
    return 0;
}

AC代码2(floyd):

#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 205;
int n,a,b,x,arr[MAXN][MAXN];

void floyd()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            for(int k=1;k<=n;k++)
            {
                if(arr[j][k]>arr[j][i]+arr[i][k])
                    arr[j][k]=arr[j][i]+arr[i][k];
            }
        }
    }
}

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    while(cin >> n && n)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(i==j) arr[i][j]=0;
                else arr[i][j]=400;
        cin >> a >> b;
        for(int i=1;i<=n;i++)
        {
            cin >> x;
            if(x+i<=n) arr[i][i+x]=1;
            if(i-x>0) arr[i][i-x]=1;
        }
        floyd();
        if(arr[a][b]==400)
            cout << -1 << endl;
        else
            cout << arr[a][b] << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41879343/article/details/102686447