这是一维的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. UNITY_MATRIX_IT_MV[Matrix] (转载)

    转载 http://blog.csdn.net/cubesky/article/details/38682975 前面发了一篇关于unity Matrix的文章. http://blog.csdn.n ...

  2. STM32 对内部FLASH读写接口函数

    因为要用内部FLASH代替外部EEPROM,把参数放在STM32的0x08000000+320K处,其中20K是bootloader,300K是应用程序. 原理:先要把整页FLASH的内容搬到RAM中 ...

  3. 翻译 - 元编程动态方法之public_send

    李哲 - MAY 20, 2015 原文地址:Metaprogramming Dynamic Methods: Using Public_send 作者:Friends of The Web的开发者V ...

  4. java与.net之间xml传递,xml最前面多了个?

    最近做一个项目,是java提供webservice供.net调用.参数采用xml格式.首先碰到的问题: 1).net这边采用XmlSerializer 方式序列化对象传递给对方.对方在本机调试可以收到 ...

  5. (转)Android之ListView原理学习与优化总结

    转自: http://jishu.zol.com.cn/12893.html 在整理前几篇文章的时候有朋友提出写一下ListView的性能优化方面的东西,这个问题也是小马在面试过程中被别人问到的….. ...

  6. vim使用详解

    1 插入类命令 i               // 在当前字符前插入 I               // 在当前行首插入 a               // 在当前字符后写入 A         ...

  7. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-005控制类型映射(Nationalized、@LOB、@org.hibernate.annotations.Type)

    一.简介 1. 2. 3. 4. to override this default mapping. The JPA specification has a convenient shortcut a ...

  8. 图解TCP/IP读书笔记(四)

    第四章.IP协议 IP(Internet Protocol,网际协议),作为整个TCP/IP中至关重要的协议,主要负责将数据包发送给最终的目标计算机.因此,IP能够让世界上任何两台计算机之间进行通信. ...

  9. Qt之界面出现、消失动画效果(简单好用)

    在学习Qt的这2.3个月里,对Qt越发感兴趣,从刚开始的盲目.无所适从到现在的学习.研究.熟练.掌握的过程中,我学到了很多东西,也学会了如何通过自学让自己更加成熟.强大起来,如何更有效地提高自己学习. ...

  10. SQL Server 高性能写入的一些总结

    1.1.1 摘要 在开发过程中,我们不时会遇到系统性能瓶颈问题,而引起这一问题原因可以很多,有可能是代码不够高效.有可能是硬件或网络问题,也有可能是数据库设计的问题. 本篇博文将针对一些常用的数据库性 ...