HDU_1548_A strange lift

题意:一部电梯(共top层),每一楼有一个数字k,在该层只能上k层或者下k层(up和down按钮),问从当前层到目标层按按钮的最小次数。

分析:广度优先搜索。

总结:初写BFS,仿照别人的代码,这方面要继续加强。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int k[205],vis[205];
int top,now,aim;
struct node
 {
     int floor,step;;
};

bool judge(int a)
{
    if(a>=1&&a<=top)
        return 1;
    return 0;
}
int BFS(node sta)
{
    queue<node>q;
    node now,next;
    vis[sta.floor]=1;
    q.push(sta);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.floor==aim)
            return now.step;
        for(int i=-1; i<=1; i+=2)
        {
            if(!vis[now.floor+i*k[now.floor]]&&judge(now.floor+i*k[now.floor]))
            {
                next.floor=now.floor+i*k[now.floor];
                next.step=now.step+1;
                vis[now.floor+i*k[now.floor]]=1;
                q.push(next);
            }
        }
    }
    return -1;
}

int main()
{

    while(scanf("%d",&top)!=EOF&&top)
    {
        scanf("%d%d",&now,&aim);
        for(int i=1; i<=top; i++)
            scanf("%d",&k[i]);
        node a;
        a.floor=now;
        a.step=0;
        memset(vis,0,sizeof(vis));
        int ans=BFS(a);
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-08 20:18:45

HDU_1548_A strange lift的相关文章

A strange lift

点击打开链接 题意:有n层楼层,现在在每一层有两个按钮,分别为up和down,按动按钮时,可以向上或向下跳动num[ i ]层:问能否以最少的次数从A到B层,不能输出-1: 解析:构图,将从i层到按动按钮后跳转的楼层,看作连通状态,赋值为1,这样就转换成单源最短路问题: #include<iostream> #include<cstring> #include<cstdio> using namespace std; const int maxn = 500; cons

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

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)

hdu1548——A strange lift

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

E - A strange lift 【BFS】

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 K

(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

杭电 1548 A strange lift(广搜)

http://acm.hdu.edu.cn/showproblem.php?pid=1548 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.T

hdu1584 A strange lift (电梯最短路径问题)

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15570    Accepted Submission(s): 5832 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