题意:

  有一座电梯,其中楼层从1~n,每层都有一个数字k,当处于某一层时,只能往上走k层,或者下走k层。楼主在a层,问是否能到达第b层?

思路:

  在起点时只能往上走和往下走两个选择,之后的每层都是这样,那么就类似于二叉树。每个节点就是对应的层,因为有可能碰到循环的层,比如1跳到3,3跳回1,这样使得无限循环,所以加个vis数组标记是否遍历过即可。

 #include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <deque>
using namespace std;
const int N=;
int n, a, b;
int q[N];
deque<int> que;
bool vis[N]; int cal()
{
if(a==b) return ;
que.clear(); int cnt=;
que.push_back(a);
while(!que.empty())
{
int siz=que.size();
for(int i=; i<siz; i++)
{
int cur=que.front();
que.pop_front();
if(cur+q[cur]==b||cur-q[cur]==b)
return cnt+; if(cur+q[cur]<=n && !vis[cur+q[cur]] ) //没有遍历过的,小于上限的才考虑
{
que.push_back(cur+q[cur]);
vis[cur+q[cur]]=;
}
if(cur-q[cur]> && !vis[cur-q[cur]] ) //没有遍历过的,大于0的才考虑
{
que.push_back(cur-q[cur]);
vis[cur-q[cur]]=;
}
}
cnt++;
}
return -;
} int main()
{
//freopen("input.txt", "r", stdin); while(cin>>n,n)
{
memset(q,,sizeof(q));
memset(vis,,sizeof(vis)); cin>>a>>b;
for(int i=; i<=n; i++)
cin>>q[i]; printf("%d\n",cal());
} return ;
}

AC代码

HDU 1548 A strange lift 奇怪的电梯(BFS,水)的更多相关文章

  1. hdu 1548 A strange lift

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

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

  3. HDU 1548 A strange lift (Dijkstra)

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

  4. HDU 1548 A strange lift (广搜)

    题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...

  5. hdu 1548 A strange lift(迪杰斯特拉,邻接表)

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

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

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

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

  8. hdu 1548 A strange lift (bfs)

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

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

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

随机推荐

  1. POJ 2181

    #include <iostream> #include <cstdio> #include <cmath> #define MAXN 150005 #includ ...

  2. Ehcache使用

    http://www.360doc.com/content/14/0423/17/16946725_371472946.shtml http://www.myexception.cn/web-appl ...

  3. 使用MbrFix.exe修复MBR分区表

    在卸载linux Ubuntu之前,先修复MBR,然后再删除Linux分区就可以了.而MbrFix.exe 就是这样一个Windows 修复MBR的应用程序软件,MbrFix.exe 不仅支持Wind ...

  4. C# Socket 入门1(转)

    1.   服务端程序  1 using System;  2 using System.Collections.Generic;  3 using System.Text;  4 using Syst ...

  5. netbeans使用

    下载地址 https://netbeans.org/downloads/ https://netbeans.org/downloads/start.html?platform=linux&la ...

  6. maven也是apache下的项目

    maven也是apache下的项目,你看maven官网了,域名都在Apache下

  7. 我们为什么需要DTO(数据传输对象)

    原文:http://www.cnblogs.com/Gyoung/archive/2013/03/23/2977233.html DTO即数据传输对象(Data Transfer Object).之前 ...

  8. Rate Limiter设计

    先存着,以后再写 http://iamzhongyong.iteye.com/blog/1982113 http://baike.baidu.com/view/2530454.htm https:// ...

  9. CentOS配置SSH单向无密码访问

    最近在研究一款文件系统,需要远程给客户机安装软件,且需要无SSH密码访问,另外需要远程给客户机传文件,每次输入root密码很不方便,就想到用ssh key生成公钥.私钥来验证,而避免每次就必须输入ro ...

  10. AlarmManager.RTC和ELAPSED_REALTIME的区别

    AlarmManager.RTC,硬件闹钟,不唤醒手机(也可能是其它设备)休眠:当手机休眠时不发射闹钟. AlarmManager.RTC_WAKEUP,硬件闹钟,当闹钟发躰时唤醒手机休眠: Alar ...