HDU 1548 A strange lift 搜索
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11341 Accepted Submission(s): 4289
go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there
is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2
th floor,as you know ,the -2 th floor isn't exist.
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"?
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.
5 1 5
3 3 1 2 5
0
3
#include<iostream>
#include<cstring>
#include<queue>
using namespace std; #define N 205
int v[N],c[N];
int n,a,b,t; struct node
{
int i,time;
}; void bfs(int x)
{
node now,tmp;
queue<node> q;
now.i=x,now.time=0;
memset(v,0,sizeof(v));
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.i==b)
{
t=0;
cout<<now.time<<endl;
return ;
}
for(int k=0;k<2;k++)
{
if(k==0) tmp.i=now.i+c[now.i];
if(k==1) tmp.i=now.i-c[now.i];
if(tmp.i>0&&tmp.i<=200&&!v[tmp.i])
{
v[tmp.i]=1;
tmp.time=now.time + 1;
q.push(tmp);
}
}
}
} int main()
{
while(cin>>n)
{
if(n==0) break;
cin>>a>>b;
for(int k=1;k<=n;k++)
cin>>c[k];
t=1;
bfs(a);
if(t) cout<<-1<<endl;
}
return 0;
}
HDU 1548 A strange lift 搜索的更多相关文章
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
- 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 (Dijkstra)
A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...
- 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)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 1548 A strange lift 题解
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1548 A strange lift (最短路/Dijkstra)
题目链接: 传送门 A strange lift Time Limit: 1000MS Memory Limit: 32768 K Description There is a strange ...
- 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 (广搜)
题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...
随机推荐
- 解决linux .so的链接时符号依赖问题
问题描述 target: a.out SO:libmyfile.so 依赖描述: a.out: libmyfile.so libmyfile.so: libssl.so.1.0.0 libssl.s ...
- 仿照淘宝首页做的一个高度伪对齐demo
功能就是当右边高度没有左边高的情况下做的一些处理,由于本人技术有限,不兼容所有浏览器, <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tra ...
- MySQL 5.6 for Windows 解压缩版配置安装(转)
转自:http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345bf0.html MySQL是一个小巧玲珑但功能强大的数据库,目前十分流行.但是官网给 ...
- php数组(array)输出三种形式
$bbbb=array("11"=>"aaa","22"=>"bbb"); //只能输出值value不能输出 ...
- jquery 获取元素的 实际宽度和高度
jquery outerHeight方法 outerWidth方法 转: http://www.cnblogs.com/zhja/archive/2012/11/07/2758665.html jqu ...
- openerp report image
webkit : 再mako 文件中插入以下代码, <% %>标签用于再mako中定义代码或者函数. 然后 ${ embed_image('图片类型', 图片字段 , 宽度,高度) } ...
- 尽量使用ToUpper比较,避免使用ToLower
在编码时尽量使用ToUpper比较,避免使用ToLower,因为微软对ToUpper进行了优化,以下为测试结果: public void TestToLower() { Stopwatch watch ...
- Nopcommerce架构浅谈之文件结构
应该是在两年前了,在拜读园子里大神的文章时偶然了解到有个叫NopCommerce的商城系统,苦于没有时间,各种耽误,其中研究过一段时间,也就是一个星期时间,后来又耽搁了,直到最近,随着项目进入间歇期, ...
- 转:PHP如何阻止用户上传成人照片或者裸照
原文来自于:http://www.hackbase.com/tech/2014-10-12/69367.html 当前,网络淫秽色情信息屡禁不止.屡打不绝,严重危害未成年人身心健康,严重败坏社会风气, ...
- wlan的QOS配置
WLAN QoS配置 1.1 WLAN QoS简介 802.11网络提供了基于竞争的无线接入服务,但是不同的应用需求对于网络的要求是不同的,而原始的网络不能为不同的应用提供不同质量的接入服务,所以已 ...