HDU - 1548 A strange lift

题目:
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

代码如下:
o(n^2)版本

#include<bits/stdc++.h>
using namespace std;
#define MAX 205
int n,a,b,maxn,aa[MAX];
int mp[MAX][MAX],dis[MAX];
bool vis[MAX];
void Dijkstra(int a)
{
    int flag;
    memset(vis,false,sizeof(vis));
    for(int i = 0;i < n;i++) dis[i] = mp[a][i];//记录起点到各个点的距离
    dis[a] = 0;//起点到到它自己距离为0
    vis[a] = true;//该点已经访问过标记为旧点
    for(int i = 0;i < n;i++){
        maxn = 0x3f3f3f3f;
        for(int j = 0;j < n;j++){
            if(!vis[j] && maxn > dis[j]){   //挑出新点中距离原点最近的点
                maxn = dis[j];
                flag = j;   //将该点记录下来
            }
        }
        if(maxn == 0x3f3f3f3f) return;//所以点都是旧点,结束
        vis[flag] = true;//将找到的里原点最近的点标记为旧点
        for(int j = 0;j < n;j++){
            if(!vis[j]){
                if(dis[j] > dis[flag] + mp[flag][j]) dis[j] = dis[flag] + mp[flag][j];
            }
        }
    }
}
int main()
{
    while(~scanf("%d",&n) && n != 0){
        cin >> a >> b;
        a--,b--;
        memset(mp,0x3f3f3f3f,sizeof(mp));
        for(int i = 0;i < n;i++){
            cin >> aa[i];
            if(i + aa[i] < n) mp[i][i + aa[i]] = 1;
            if(i - aa[i] >= 0) mp[i][i - aa[i]] = 1;
        }
        Dijkstra(a);
        if(dis[b] == 0x3f3f3f3f) cout << "-1" << endl;
        else cout << dis[b] << endl;
    }
    return 0;
}

o(nlogn)版本

#include<bits/stdc++.h>
using namespace std;
#define MAX 205
int n,a,b,aa[MAX];
int mp[MAX][MAX],dis[MAX];
bool vis[MAX];
struct Node
{
    int num,x;
    Node(int num2,int xx){num = num2;x = xx;}
    bool operator < (const Node &node) const{
        return x > node.x;
    }
};
void Dijkstra(int a)
{
    int flag;
    memset(vis,false,sizeof(vis));
    memset(dis,0x3f3f3f3f,sizeof(dis));
    priority_queue<Node> q;
    Node node(a,0);
    q.push(node);
    dis[a] = 0;
    while(!q.empty()){
        Node node = q.top();
        q.pop();
        flag = node.num;
        if(vis[flag]) continue;
        vis[flag] = true;
        for(int i = 0;i < n;i++){
            if(mp[flag][i] == 0x3f3f3f3f) continue;
            if(dis[i] > dis[flag] + mp[flag][i]){
                dis[i] = dis[flag] + mp[flag][i];
                Node node(i,dis[i]);
                q.push(node);
            }
        }
    }
}
int main()
{
    while(~scanf("%d",&n) && n != 0){
        cin >> a >> b;
        a--,b--;
        memset(mp,0x3f3f3f3f,sizeof(mp));
        for(int i = 0;i < n;i++){
            cin >> aa[i];
            if(i + aa[i] < n) mp[i][i + aa[i]] = 1;
            if(i - aa[i] >= 0) mp[i][i - aa[i]] = 1;
        }
        Dijkstra(a);
        //for(int i = 0;i < n;i++) cout << dis[i] << endl;
        if(dis[b] == 0x3f3f3f3f) cout << "-1" << endl;
        else cout << dis[b] << endl;
    }
    return 0;
}

题意:
先输入电梯层数n,然后输入起点和终点位置。然后每一层有一个数字(假设是k),这代表从这一层你只能往上k层或者是往下k层(每次不管往上还是往下都需要按一次按钮),当然往上不能超过总数n,往下不能小于0,最后要你计算从起点到终点一共需要按几次按钮。
思路:
一开始是想用bfs的,因为最近在学最短路,所以就用了 Dijkstra算法,上述代码已有解释。这里就不多说了。

刚刚打完第十届蓝桥杯,个人感觉不太好,休息了1天,还是继续做题吧…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值