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 ...
随机推荐
- 蓝色巨人IBM
1911年IBM的前身CRT建立,在中华民国时期就与中国有很多商业合作,中国中央银行,中国银行,黄埔造船厂,建国后直到中美建交,IBM与中国的关系越来越紧密,今晚看了一遍关于蓝色巨人的视频,收益匪浅. ...
- Postman高级应用——流程控制、调试、公共函数、外部数据文件
postman客户端下载地址:https://www.getpostman.com/apps 目录 流程控制 调试 公共函数 外部数据文件 流程控制 流程控制简言之就是设置接口的执行顺序,流程控制只有 ...
- 使用node.js检查js语法错误
如果没有一些工具和插件写JavaScript代码遇到语法错误找起来很费时间,请教了同事怎么用node.js检查 用浏览器测试的时候报语法错误. 1.点击红圈中的蓝色按钮,下次刷新是会在抛出异常的时候自 ...
- Linux学习——shell编程之变量
shell编程之变量:Linux shell编程基础中的变量. 包括Bash变量的分类和各变量的详细使用,如:用户自定义变量.环境变量.语系变量.位置参数变量和预定义变量. 1:什么是Bash变量? ...
- Lowbit Sum 规律
Lowbit Sum Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitSt ...
- Robberies hdu 2955 01背包
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 全方位解读及介绍windows网络安全及常见攻击方式
本来我就是来逛逛论坛的,可是看到前面有位一样是干网络安全的同行,留下来过的痕迹,发了一篇相对不错的文章,我寻思咱既然来这一趟,也不能显得就比别人差啊.所以我也就写了这一片不算成熟小文章,望各位共勉之哈 ...
- jq 通过标签属性匹配标签
有时候会有这样的需求 在一堆的标签中定位到某个标签去获取值 这时候我们就可以通过使用自定义属性将这一堆标签打上不同的标记 通过属性定位标签了 下面是代码: <input class=" ...
- hadoop配置文件详解,安装及相关操作
一. Hadoop伪分布配置 1. 在conf/hadoop-env.sh文件中增加:export JAVA_HOME=/home/Java/jdk1.6 2. 在conf/core-s ...
- c# 多线程传递参数以及任务
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...