A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13724 Accepted Submission(s): 5239
is a 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"?
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.
each case 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".
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int N,A,B;
int flor[250];
int dir[]={-1,1};//上下方向
struct node{
int x;//楼层
int step_cnt;//步数
};
node now,nextf;
node vs,vd;//起点和终点
int leap;
int vis[250];//标记数组只需一维,即标记刚入队的nextf,下次不用再回到此处,
//因你到了nextf结点后,只会有上下两个选择,即使下次再回到此处
//面对的仍是上下两个选择,而在第一次就可抉择
void in_put()
{
scanf("%d%d",&A,&B);
for(int i=1;i<=N;++i)
scanf("%d",&flor[i]);
vs.x=A;
vd.x=B;
}
void bfs()
{
queue<node>Q;
vs.step_cnt=0;
Q.push(vs);
while(!Q.empty()){
now=Q.front();
Q.pop();
if(now.x==vd.x) {leap=1;return;}
for(int i=0;i<2;++i){
nextf.x=now.x+dir[i]*flor[now.x];
nextf.step_cnt=now.step_cnt+1;
if(nextf.x>=1&&nextf.x<=N&&!vis[nextf.x]) {Q.push(nextf);vis[nextf.x]=1;}//迷之wa,原因在于写出了vis[now.x]=1;
}
}
}
int main()
{
while(scanf("%d",&N)&&N){
leap=0;
memset(vis,0,sizeof(vis));
in_put();
bfs();
if(leap)
printf("%d\n",now.step_cnt);
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 ...
- 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 ...
随机推荐
- web_url、web_custom_request、web_submit_data、web_submit_form的使用实例
业务:根据url获取图片用web_url()函数访问数据;请求方式:HTTP GET 请求;Action(){web_url("imageproxytest", "URL ...
- unity3d的四元数 Quaternion
原地址:http://www.cnblogs.com/88999660/archive/2013/04/02/2995074.html 今天准备学习和研究下unity3d的四元数 Quaternion ...
- navicat连接oracle报错ORA-12737: Instant Client Light: unsupported server character set CHS16GBK”
原文如下http://blog.163.com/cp7618@yeah/blog/static/7023477720142154449893/?COLLCC=1318255100& 这个工具可 ...
- Android判断网络是否连接
<!-- 配置文件判断网络是否连接 --> <uses-permission android:name="android.permission.ACCESS_NETWORK ...
- 找不到提交和更新按钮,subversion不见了,无法更新和上传代码
1.查看settings/plugins/下有没有subversion 插件,如果有,确保勾上. 2.VCS->Enable Version Control Integration...
- BADIP filter
#!/bin/bash touch /tmp/badipnew.log;touch /tmp/newip.log; if [ ! -f "/tmp/badip.log" ];the ...
- PHP编译支持mysqli
PHP编译支持mysqli前提是必须安装mysql直接上命令先进入源码包我的源码包是在/usr/local/php-5.2.1/ext/mysqli这样进入 cd /usr/local/php-5.2 ...
- http协议学习系列
深入理解HTTP协议(转) http://www.blogjava.net/zjusuyong/articles/304788.html http协议学习系列 1. 基础概念篇 1.1 介绍 H ...
- Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- Xenomai
http://blog.csdn.net/robertsong2004/article/details/43889249 嵌入式系统的开发,如果对实时性要求不高,就可以使用Linux自身的实时补丁实现 ...