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学习经历24
Fragment生命周期 1.onAttach() 2.onCreate() 3.onCreateView() 4.onActivityCreated() 5.onStart() 6.onResume ...
- 动态加载JS
<script> Date.prototype.format = function(format) { var o = { , //month "d+" : this. ...
- c++ stl string char* 向 string 转换的问题
请看下面代码 string AddString(const string& a,const string & b) { return a + b; } int _tmain(int a ...
- linux、windows下导入、导出mysql数据库命令
一.导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径): 1.导出数据和表结构:[不是mysql里的命令]mysqldump -u用户名 -p密码 数据库名 > 数据 ...
- Practical Java
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- SQL判断临时表是否存在
IF EXISTS(select * from tempdb..sysobjects where id=object_id('tempdb..#tb')) BEGIN DROP TABLE #tb E ...
- MySQL多表更新(逻辑外键/事实外键)
语法结构: UPDATE table_reference SET 列名1=value1[,列名2=value2,......] [WHERE where_condition] 说明: tabl ...
- 线程入门之yield
package com.thread; /** * <yiedl:把cpu让给其他线程> * <功能详细描述> * * @author 95Yang */ public cla ...
- mysql 2003 10038 连接不上的解决
网上写的很复杂,其实解决办法是 你在右键管理员权限下运行 mysqld. 忘记这个了,害的又找了近1个小时的时间找问题.
- mysq错误(1)空用户创建库
mysql5.6.24免安装版: 1.ERROR 1044 (42000): Access denied for user ''@'localhost' to database 现象:创建库失败. 出 ...