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

分析:广度优先搜索。

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

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int k[],vis[];
int top,now,aim;
struct node
{
int floor,step;;
}; bool judge(int a)
{
if(a>=&&a<=top)
return ;
return ;
}
int BFS(node sta)
{
queue<node>q;
node now,next;
vis[sta.floor]=;
q.push(sta);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.floor==aim)
return now.step;
for(int i=-; i<=; i+=)
{
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+;
vis[now.floor+i*k[now.floor]]=;
q.push(next);
}
}
}
return -;
} int main()
{ while(scanf("%d",&top)!=EOF&&top)
{
scanf("%d%d",&now,&aim);
for(int i=; i<=top; i++)
scanf("%d",&k[i]);
node a;
a.floor=now;
a.step=;
memset(vis,,sizeof(vis));
int ans=BFS(a);
printf("%d\n",ans);
}
return ;
}

HDU_1548_A strange lift的更多相关文章

  1. HDU 1548 A strange lift (最短路/Dijkstra)

    题目链接: 传送门 A strange lift Time Limit: 1000MS     Memory Limit: 32768 K Description There is a strange ...

  2. HDU 1548 A strange lift (bfs / 最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...

  3. A strange lift

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  4. bfs A strange lift

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at e ...

  5. hdu 1548 A strange lift

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...

  6. hdu 1548 A strange lift 宽搜bfs+优先队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...

  7. HDU 1548 A strange lift (Dijkstra)

    A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...

  8. HDU1548——A strange lift(最短路径:dijkstra算法)

    A strange lift DescriptionThere is a strange lift.The lift can stop can at every floor as you want, ...

  9. HDU 1548 A strange lift 搜索

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

随机推荐

  1. sharepoint 訪问缩略图

    Sharepoint缩略图 简单介绍 Sharepoint2010中有专门的图片库,当你新建图片库后,向图片上传一部分图片.当你浏览这个库时显示一排排小图片.当点击一个图片时进入显示的是大图.不要简单 ...

  2. ImportError: No module named &#39;ConfigParser&#39;

    Resolve Method: I found the problem. I had manually installed a newer version of python (version 3.2 ...

  3. Netty In Action中文版 - 第四章:Transports(传输)

    本章内容 Transports(传输) NIO(non-blocking IO,New IO), OIO(Old IO,blocking IO), Local(本地), Embedded(嵌入式) U ...

  4. poj 3263 Tallest Cow(线段树)

    Language: Default Tallest Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1964   Ac ...

  5. 树状数组 LA 4329 亚洲赛北京赛区题

    复习下树状数组 还是蛮有意思的一道题: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&cat ...

  6. 二分lower_bound()与upper_bound()的运用

    <span style="color:#6633ff;">/* G - 二分 Time Limit:2000MS Memory Limit:32768KB 64bit ...

  7. java7-Fork/Join

    Fork/Join 框架与传统线程池的区别采用“工作窃取”模式(work-stealing):当执行新的任务时它可以将其拆分分成更小的任务执行,并将小任务加到线程队列中,然后再从一个随机线程的队列中偷 ...

  8. NSubstitute

    https://github.com/nsubstitute/NSubstitute http://nsubstitute.github.io/help/creating-a-substitute/

  9. 响应在此上下文中不可用 asp.net

    (一)实例1: 在asp.net程序中添加了一个 类.cs 如下 using System; using System.Collections; using System.ComponentModel ...

  10. openStack集群封装记录