hdu A strange lift

有起点和终点,有方向,有最少次数,所以这道题很明显是一道bfs的题目,这题要利用vist数组来标记已走过的楼层,因为这题里面已走过的楼层是不可能在走第二遍的。

第二次走和第一次走的选择没有任何的区别。

#include"iostream"
#include"stdio.h"
#include"string.h"
#include"algorithm"
#include"cmath"
#include"queue"
#define mx 205
using namespace std;
int n,a,b;
int k[mx];
int vist[mx];
struct node
{
    int x,times;
    friend bool operator<(node t1,node t2)
    {
        return t2.times<t1.times;
    }
};
bool judge(int x)
{
    if(x>=1&&x<=n&&!vist[x]) return true;
    return false;
}
void bfs()
{
    node cur,next;
    int i;
    cur.x=a;cur.times=0;
    priority_queue<node>q;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        if(cur.x==b){cout<<cur.times<<endl;return;}
        for(i=0;i<2;i++)
        {
            next.x=cur.x+pow(-1,i)*k[cur.x];
            if(judge(next.x))
            {
                next.times=cur.times+1;
                vist[next.x]=1;
                q.push(next);
            }
        }
    }
    cout<<-1<<endl;
}
int main()
{
    while(cin>>n,n)
    {
        int i;
        cin>>a>>b;
        for(i=1;i<=n;i++) cin>>k[i];
        memset(vist,0,sizeof(vist));
        vist[a]=1;bfs();
    }
    return 0;
}

时间: 2024-10-14 06:00:46

hdu A strange lift的相关文章

HDU 1548 A strange lift【不错的最短路,spfa】

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 21281    Accepted Submission(s): 7786 Problem Description There is a strange lift.The lift can stop can at every floor as you want

HDU 1548 A strange lift(Dijkstra,简单BFS)

题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数,然后是n个数分别代表第i层的移动范围.输出最少移动次数,若不可达,输出-1. 解题思路: 1.用Dijkstra算法,首先构建邻接矩阵,注意在构造时,要考虑i-k[i]<1和i+k[i]>n,i代表当前所在层. 1 #include<string.h> 2 #include<st

HDU 1548 (最基础的BFS了) A strange lift

这是一维的BFS,而且没有什么变形,应该是最基础的BFS了吧 题意: 有这样一个奇葩的电梯,你在第i层的时候你只能选择上或者下Ki层,也就是你只能从第i层到达i+Ki或者i-Ki层.当然电梯最低只能在1层最高只能在n层. 给出起点和终点问最少需要多少次才能到达终点,如果不能到达输出-1 没有什么好解释的了,如此单纯的一道题,只要不是太粗心就能A过去 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #incl

hdu 1548 A strange lift (dijkstra算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 题目大意:升降电梯,先给出n层楼,然后给出起始的位置,即使输出从A楼道B楼的最短时间. 注意的几点 (1)每次按一下,只能表示上或者是下,然后根据输入的看是上几层或者是下几层. (2)注意不能到底不存在的楼层. 详见代码. 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 const int IN

HDU 1548 A strange lift 搜索

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11341    Accepted Submission(s): 4289 Problem Description There is a strange lift.The lift can stop can at every floor as you want

HDU 1548.A strange lift

A strange lift Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 1548 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)

(hdu step 4.2.4)A strange lift(求从起点到终点的最小步数,限制条件是:在一维的情况下)

题目: A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 709 Accepted Submission(s): 348   Problem Description There is a strange lift.The lift can stop can at every floor as you want, a

HDU 搜索练习 A strange lift

A strange lift Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 122 Accepted Submission(s) : 22 Problem Description There is a strange lift.The lift can stop can at every floor as you want, and ther

HDU 1548 A strange lift(最短路&amp;&amp;bfs)

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26943    Accepted Submission(s): 9699 Problem Description There is a strange lift.The lift can stop can at every floor as you want,