这是一维的BFS,而且没有什么变形,应该是最基础的BFS了吧

题意:

有这样一个奇葩的电梯,你在第i层的时候你只能选择上或者下Ki层,也就是你只能从第i层到达i+Ki或者i-Ki层。当然电梯最低只能在1层最高只能在n层。

给出起点和终点问最少需要多少次才能到达终点,如果不能到达输出-1

没有什么好解释的了,如此单纯的一道题,只要不是太粗心就能A过去

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = + ;
struct Point
{
int h;
int times;
}qu[maxn];
int n, from, to, go[maxn], vis[maxn];
int head, tail;
int dir[] = {, -}; void BFS(void)
{
head = , tail = ;
qu[].h = from, qu[].times = ;
while(head < tail)
{
if(qu[head].h == to)
{
printf("%d\n", qu[head].times);
return;
}
for(int i = ; i < ; ++i)
{
int hh = qu[head].h + go[qu[head].h]*dir[i];
if(hh> && hh<=n && (!vis[hh]))
{
vis[hh] = ;
qu[tail].h = hh;
qu[tail++].times = qu[head].times + ;
}
}
++head;
}
printf("-1\n");
} int main(void)
{
#ifdef LOCAL
freopen("1548in.txt", "r", stdin);
#endif while(scanf("%d", &n) == && n)
{
scanf("%d%d", &from, &to);
for(int i = ; i <= n; ++i)
scanf("%d", &go[i]);
memset(vis, , sizeof(vis));
BFS();
}
return ;
}

代码君

HDU 1548 (最基础的BFS了) A strange lift的更多相关文章

  1. 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 ...

  2. hdu 1548 楼梯 bfs或最短路 dijkstra

    http://acm.hdu.edu.cn/showproblem.php?pid=1548 Online Judge Online Exercise Online Teaching Online C ...

  3. 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 ...

  4. hdu 1548 A strange lift (bfs)

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

  5. HDU 1548 A strange lift(BFS)

    Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...

  6. HDU 1548 A strange lift(最短路&&bfs)

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

  7. hdu 1240:Asteroids!(三维BFS搜索)

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

  8. hdu 1548 A strange lift

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

  9. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

随机推荐

  1. Java 延时常见的几种方法

    1. 用Thread就不会iu无法终止 new Thread(new Runnable() { public void run() { while (true) { test(); try { Thr ...

  2. HDU Destroy Transportation system(有上下界的可行流)

    前几天正看着网络流,也正研究着一个有上下界的网络流的问题,查看了很多博客,觉得下面这篇概括的还是相当精确的: http://blog.csdn.net/leolin_/article/details/ ...

  3. ZJU 1180 Self Numbers(暴力模拟判断,水题)

    题目链接 同HDU 1128 , POJ 1316(这个范围小一点). 原本怕超时,以为有技巧或者规律,死命的想,后来发现这就是一道水体,模拟着全部判断一下就好了,10秒呢,完全不怕超时...唔,废话 ...

  4. 相对布局RelativeLayout

      一. public class RelativeLayout extends ViewGroup java.lang.Object    ↳ android.view.View      ↳ an ...

  5. 处理HTTP状态码

    1.1.4  处理HTTP状态码 上一节介绍HttpClient访问Web资源的时候,涉及HTTP状态码.比如下面这条语句: int statusCode=httpClient.executeMeth ...

  6. 从idea到ipo

    **************************************************************************************************** ...

  7. JMeter使用指南--转

    JMeter使用指南 本文重点介绍JMeter工具在测试中地位以及其中一些难以理解或者手册中含糊不清的感念,读者可以通过本文了解这些概念,然后再根据自己的需要查阅JMeter中各个组件的具体用法来完成 ...

  8. 【HDU 5030】Rabbit's String (二分+后缀数组)

    Rabbit's String Problem Description Long long ago, there lived a lot of rabbits in the forest. One d ...

  9. Java Applet与Java Application的特点

    java application是应用程序,用于桌面开发,java applet是小应用程序,一般嵌入到网页里运行.applet一般用于B/S页面上作为插件式的开发,而application主要是桌面 ...

  10. Spring框架学习之第4节

    从ApplicaionContext应用上下文容器中获取bean和从bean工厂容器中有什么区别: 具体案例如下 结论: 1.如果使用上下文ApplicationContext,则配置的bean如果是 ...