题意:给出一个电梯,给出它的层数f,给出起点s,终点g,以及在每一层能够上或者下w[i]层,问至少需要按多少次按钮到达终点。

和POJ catch that cow一样,直接用了那一题的代码,发现一直wa,

后来才发现,POJ catch that cow是单组输入的,所以每次调用的时候不用清空队列,而这一题一次输入有多组数据---

用这个清空队列

while(!q.empty()) q.pop();

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 100005
using namespace std;
queue<int> q;
int visit[maxn],step[maxn],n,k,w[maxn],flag,t,sum;
void bfs()
{
int head,next,i;
while(!q.empty()) q.pop(); //注意调用前一定要清空
q.push(n);
visit[n]=1;
step[n]=0;
while(!q.empty())
{
head=q.front(); q.pop();//取出 并弹出队首
for(i=1;i<=2;i++)
{
if(i==1) next=head-w[head];
else next=head+w[head];
if(next>t||next<1) continue;//越界 if(!visit[next])//判重
{
q.push(next);
visit[next]=1;
step[next]=step[head]+1;
if(next==k) //找到终点
{
flag=1;
sum=step[next];
return;
}
}
}
}
return;
}
int main()
{
int i;
while(scanf("%d",&t)!=EOF&&t)
{
memset(visit,0,sizeof(visit));
// memset(step,0,sizeof(step));//不用清空step数组 scanf("%d %d",&n,&k);
for(i=1;i<=t;i++)
scanf("%d",&w[i]);
if(n==k)
{
printf("0\n");
continue;
}
flag=0;
bfs();
if(flag) printf("%d\n",sum);
else printf("-1\n");
}
}

  

HDU 1548 A strange lift【BFS】的更多相关文章

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

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

  3. hdu 1548 A strange lift

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

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

  5. hdu 1548 A strange lift (bfs)

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

  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 1548 A strange lift (Dijkstra)

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

  8. HDU 1548 A strange lift 搜索

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

  9. HDU 1548 A strange lift (广搜)

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

随机推荐

  1. jQuery分页插件pagination的用法

    https://www.zhangxinxu.com/jq/pagination_zh/ 参数: 参数名 描述 参数值 maxentries 总条目数 必选参数,整数 items_per_page 每 ...

  2. vue中使用Ueditor编辑器 -- 1

    一.   下载包: 从Ueditor的官网下载1.4.3.3jsp版本的Ueditor编辑器,官网地址为:http://ueditor.baidu.com/website/download.html ...

  3. javascript中缓存

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. windows 2008 中IIS7.0以上如何设置404错误页面

    404错误页面的设置,不仅仅可以提高用户体验度,从SEO方面考虑,也是非常重要的.今天,笔者在这里介绍一下在windows 2008下如何设置404错误页面. 注意:设置404有我这里介绍2种方式,推 ...

  5. memcache session共享问题(ubuntu)

    memcache session共享问题 环境:三台ubuntu 12.04.5虚拟机,均安装php-fpm,并重用了之前搭建的简单的负载均衡 u1(192.168.240.130)    u2(19 ...

  6. CF1038D Slime 构造

    题目大意: 有nnn只史莱姆qwq,每只史莱姆有一个分数,每次一只史莱姆可以吞掉左边的或者右边的史莱姆(要是有的话),然后ta的分数会减去被吞的史莱姆的分数,问最后剩下的史莱姆分数最大为多少 输入格式 ...

  7. Facebook 运维内幕曝光:一人管理2万台服务器

    Facebook 运维内幕曝光:一人管理2万台服务器 oschina 发布于: 2013年11月23日 (29评) 分享到  新浪微博腾讯微博 收藏+32 11月30日 珠海 源创会,送U盘,先到先得 ...

  8. 安装node-sass及报错后解决方案

    使用npm install 命令安装node-sass时,经常出现安装失败的情况.原因在于npm服务器在美国,还有就是某强大的防火墙作用.导致模块无法下载. 1 npm install node-sa ...

  9. IIC 原理讲解

    IIC具体是什么这里我就不细说了,只收集一些关于IIC的原理. IIC总线优点是节约总线数,稳定,快速, 是目前芯片制造上非常 流行的一种总线,大多数单片机已经片内集成了IIC总线接口,无 需用户自己 ...

  10. 2015 Multi-University Training Contest 1 hdu 5296 Annoying problem

    Annoying problem Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...