题意:给出一个电梯,给出它的层数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. ValueProvider核心的值提供系统

    Model绑定的数据具有多种来源: 提交的表单 Json字符串 当前路由数据 请求地址的查询字符串 ASP.NET MVC将这种基于不同数据来源的数据提供机制实现在ValueProvider的组件中 ...

  2. python 3.x 学习笔记16 (队列queue 以及 multiprocessing模块)

    1.队列(queue) 用法: import queue q = queue.Queue() #先进先出模式 q.put(1) #存放数据在q里 作用: 1)解耦    2)提高效率 class qu ...

  3. DirectUI界面编程(二)绘制一个按钮

    上节介绍了使用源码方式构建Duilib应用的项目配置,并创建了一个最简单的基于Duilib库的窗口,细心的读者会发现,当我们点击窗口的关闭按钮时,应用并没有真的退出,因为我们并没有对窗口事件进行处理, ...

  4. Android实现图片相似度

    Android实现图片相似度 最近公司有一个需求,就是希望能判断用户提交的照片是否是身份证的正面或者反面.可以通过预设一张拍摄清晰的身份证正面或者反面,来对比是否相似,那么问题就转化为如何计算两张图片 ...

  5. C#操作IIS服务

    进入正题:先从使用角度来讲解IIS操作,然后再深入到具体的IIS服务底层原理. [1]前提掌握要点: (1).IIS到目前经历了四个版本分别为 IIS4.0 IIS5.0 IIS6.0 IIS7.0, ...

  6. apache include 文件包含引用的方法 报错 [an error occurred while processing this directive]

    今天遇到在某平台买的虚拟主机服务器不支持    下面的这样的写法 <!--#Include file="/templets/2013new/header.htm"--> ...

  7. EFCore笔记之异步查询

    当在数据库中执行查询时,异步查询可避免阻止线程. 这有助于避免冻结富客户端应用程序的 UI.异步操作还可以增加 Web 应用程序的吞吐量,可以在数据库操作完成时释放线程去处理其他请求. Entity ...

  8. 二、frps 完整配置文件

    # [common] is integral section [common] # A literal address or host name for IPv6 must be enclosed # ...

  9. java判断输入的数字的位数_数字问题

    import java.util.Scanner;public class Numbers { public void Judgy(int n){ for(int i=0;i<100;i++){ ...

  10. 11g,12c Oracle Rac安装

    安装 Oracle 12cR1 RAC on Linux 7 本文介绍如何在Oracle Linux 7上安装2节点Oracle 12cR1 Real Application Cluster(RAC) ...