题意:给出一个电梯,给出它的层数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. DIV水平方向居中的几种方法

    一.使用margin: 1 #center0 { 2 background: red; 3 margin: 0 auto; 4 } 或者: margin: auto; 这样的前提是父盒子里没有其他盒子 ...

  2. struts2学习之基础笔记3

    第8章Struts 2类型转换 使用类型转换器 自定义类型转换器 步骤:1. Struts 2 构建流程 2.自定义类型转换器类(继承 DefaultTypeConverter /StrutsType ...

  3. 一个基于Vue.js+Mongodb+Node.js的博客内容管理系统

    这个项目最初其实是fork别人的项目.当初想接触下mongodb数据库,找个例子学习下,后来改着改着就面目全非了.后台和数据库重构,前端增加了登录注册功能,仅保留了博客设置页面,但是也优化了. 一.功 ...

  4. table-layout:fixed属性

    说实话,第一次见到这个属性有点懵圈,真是没见过这个属性 好了,直接说作用 table-layout其实是一种表格算法,用来显示表格单元格.行.列的算法规则. 固定表格布局: 固定表格布局与自动表格布局 ...

  5. Sona && Little Elephant and Array && Little Elephant and Array && D-query && Powerful array && Fast Queries (莫队)

    vjudge上莫队专题 真的是要吐槽自己(自己的莫队手残写了2个bug) s=sqrt(n) 是元素的个数而不是询问的个数(之所以是sqrt(n)使得左端点每个块左端点的范围嘴都是sqrt(n)) 在 ...

  6. Day 04 [与用户交互,格式化输出,基本运算符]

    Python 的与用户交互 name=input("请输入姓名:") height=input('请输入身高:') weight=input('请输入体重:') 在python3中 ...

  7. APICloud资料

    //语音读text里面的文字 var text=document.getElementById('ready').value; alert(text); var obj = api.require(' ...

  8. Multipartfile与File类型相互转换

    特殊情况下需要做转换 1.M转F File file = new File(path); FileUtils.copyInputStreamToFile(multipartFile.getInputS ...

  9. phpexcel乱码问题

    php导出Excel乱码,只需在header函数前加入ob_end_clean();//清除缓冲区,避免乱码

  10. 【codeforces 810A】Straight «A»

    [题目链接]:http://codeforces.com/contest/810/problem/A [题意] 有n门课的成绩,和一个整数k代表每门课的满分都是k分; 然后这n门课的成绩是按照平均分算 ...