A strange lift
strange lift.The lift can stop can at every floor as you want, and
there is a number Ki(0 <= Ki <= N) on every floor.The lift
have just two buttons: up and down.When you at floor i,if you press
the button "UP" , you will 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"?
consists of several test cases.,Each test case contains two
lines.
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.
of the input output a interger, the least times you have to press
the button when you on floor A,and you want to go to floor B.If you
can't reach floor B,printf "-1".
5
#include
#include
#include
#include
#include
#define maxn 205
using namespace std;
int A,B,n,flo,a,f;
int k[maxn],t[maxn];
bool visit[maxn];
int check(int a)
{
if(a<0||a>n||visit[a])
return 1;
else
return 0;
}
int bfs(int A,int B)
{
queueQ;
Q.push(A);
visit[A]=true;
while(!Q.empty())
{
flo=Q.front();
Q.pop();
if(flo==B)
{
f=1;
return t[flo];
}
//上楼
a=flo+k[flo];
if(!check(a))
{
Q.push(a);
t[a]=t[flo]+1;
visit[a]=true;
}
//下楼
a=flo-k[flo];
if(!check(a))
{
Q.push(a);
t[a]=t[flo]+1;
visit[a]=true;
}
}
}
int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d",&n)&&n)
{
memset(visit,false,sizeof(visit));
memset(t,0,sizeof(t));
f=0;
scanf("%d%d",&A,&B);
flo=A;
for(int i=1;i<=n;i++)
scanf("%d",&k[i]);
bfs(A,B);
if(f)
printf("%d\n",t[B]);
else
printf("-1\n");
}
}
A strange lift的更多相关文章
- 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 / 最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...
- A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- bfs A strange lift
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at e ...
- 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 ...
- HDU1548——A strange lift(最短路径:dijkstra算法)
A strange lift DescriptionThere is a strange lift.The lift can stop can at every floor as you want, ...
- HDU 1548 A strange lift 搜索
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu 1548 A strange lift (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- 在JavaScript中使用json.js:使得js数组转为JSON编码
在json的官网中下载json.js,然后在script中引入,以使用json.js提供的两个关键方法. 1.数组对象.toJSONString() 这个方法将返回一个JSON编码格式的字符串,用来表 ...
- 《算法导论》学习总结 — XX.第23章 最小生成树
一.什么叫最小生成树 一个无向连通图G=(V,E),最小生成树就是联结所有顶点的边的权值和最小时的子图T,此时T无回路且连接所有的顶点,所以它必须是棵树. 二.为什么要研究最小生成树问题 <算法 ...
- php中常用的字符串比较函数strcmp()实例解释
int strcmp ( string $str1 , string $str2 ) 以二进制方式进行比较以该函数比较时区分大小写返回值,如果str1小于str2返回<0,如果str1大于str ...
- windows访问控制列表 --ACL(Access Control List)
1.定义 ACL是一个windows中的表示用户(组)权限的列表. Access Control List(ACL) Access Control Entry(ACE) ... 2.分类 ACL分为两 ...
- 在项目中创建单元测试时junit的配置和使用
首先配置项目中AndroidMainfest.xml文件,加入 <instrumentation android:name="android.test.InstrumentationT ...
- DDL DML DCL TCL之不同
http://www.orafaq.com/faq/what_are_the_difference_between_ddl_dml_and_dcl_commands DDL Data Definiti ...
- ServletListener对象学习笔记
JavaWeb学习笔记--监听器详解 知识概要: 1.监听器下例子举例 2.Servlet规范中的监听器 3. 4. 1. 监听器下例子举例说明: /* Frame:事件源.发生事件的对象 Windo ...
- ItemsPanelTemplate的用法
项目里想用Silverlight制作工具栏,之前用的是Image和TextBlock完成的,但是代码混乱,在后来版本中突然想到ListBox可以实现这样的效果.使用后效果确实不错.下面是我的笔记 &l ...
- jQuery form插件使用详解
点击打开: jquery选择器全解 jquery中的style样式操作 jquery中的DOM操作 jquery中的事件操作全解 jquery中的动画操作全解 jquery中ajax的应用 自定义jq ...
- 跨域资源共享CORS实现
问题描述: 本地已经实现的restful接口,在地址栏输入url:loaclhost:8080/admins即可得到预期的json字符串,在网页上显示如下: [{"id":1,&q ...