bfs A strange lift
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1548
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button "UP" or "DOWN"?
Input
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.
Output
Sample Input
0
Sample Output
#include<iostream>
#include<queue> using namespace std; int k[],flag[];
int n,a,b; struct node{
int floor;
int step;
}; int bfs(){
node cur,next;
queue<node> q;
cur.floor = a;
cur.step = ;
flag[a] = ;//最开始把这个给忘记了还WA了一次,我傻!
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
if(cur.floor == b){
return cur.step;
}
if(cur.floor+k[cur.floor]<=n && cur.floor+k[cur.floor]> && !flag[cur.floor+k[cur.floor]]){
next.floor = cur.floor + k[cur.floor];
flag[cur.floor] = ;
next.step = cur.step + ;
q.push(next);
}
if(cur.floor-k[cur.floor]> && cur.floor-k[cur.floor]<=n && !flag[cur.floor-k[cur.floor]]){
next.floor = cur.floor - k[cur.floor];
flag[cur.floor] = ;
next.step = cur.step + ;
q.push(next);
}
}
return -;
} int main(){
while(cin>>n,n){
cin>>a>>b;
for(int i=;i<=n;i++){
cin>>k[i];
flag[i]=;
}
cout<<bfs()<<endl;
}
return ;
}
bfs A strange lift的更多相关文章
- 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 ...
- 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 ...
- hdu 1548 A strange lift (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU1548- A strange lift (BFS入门)
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A Strrange lift Time Limit: 2000/1000 MS (Java/ ...
- 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 ...
- 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 ...
- E - A strange lift 【数值型BFS+上下方向】
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 ...
- HDU 1548 A strange lift(最短路&&bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
随机推荐
- android导入项目出现style错误,menu错误
android导入项目出现style错误,menu错误 style //查看 res/values/styles.xml 下的报错点. <style name="AppBaseThem ...
- SqlSever基础 detalength函数 字节的个数
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- UE4编程之C++创建一个FPS工程(二)角色网格、动画、HUD、子弹类
转自:http://blog.csdn.net/u011707076/article/details/44243103 紧接上回,本篇文章将和大家一同整理总结UE4关于角色网格.动画.子弹类和HUD的 ...
- Why And When To Use Pre-Update and Pre-Insert Triggers In Oracle Forms
Whenever we commit after entering data in Oracle Forms, many triggers fires during this event and al ...
- Django基础-过滤器
1.可以通过过滤器来修改变量的显示,过滤器的形式是:{{ variable | filter }},管道符号'|'代表使用过滤器 2.过滤器能够采用链式的方式使用,例如:{{ text | escap ...
- Linux链接库一(动态库,静态库,库放在什么路径下)
http://www.cppblog.com/wolf/articles/74928.html http://www.cppblog.com/wolf/articles/77828.html http ...
- IIS网站部署注意点
在IIS上部署网站时,除了在添加网站时配置好相关程序池,主目录,安全性,选择.Netframwork版本这些步骤外, 容易忘记的是有些网站需要打开web服务扩展.
- SQL生成包含年月日的流水号
--************************************************************************************************** ...
- JVM 1.类的加载、连接、初始化
Java类的加载是由类加载器来完成的,过程如下: 首先,加载是把硬盘.网络.数据库等的class文件中的二进制数据加载到内存的过程,然后会在Java虚拟机的运行时数据区的堆区创建一个Class对象,用 ...
- 线程入门之join方法
package com.thread; /** * <join:将某线程加入进来,相当于方法调用,也叫合并某个线程> * <功能详细描述> * * @author 95Yang ...