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 ...
随机推荐
- 两周“学会”bootstrap搭建一个移动站点
一直想着用bootstrap搭建网站,它的自适应.元素封装完善.现成的Glyphicons字体图标,省去很多的css.js.ui的工作,可以快速搭建一个客户需要的站点.ytkah自己有一些div+cs ...
- Java中的final修饰符
1.什么时候可以选择final修饰符 如果想让一个类不被其他类继承,不允许在有子类,这时候就要考虑用到final来修饰. 2.用final修饰的类 首先大家要明白,用final修饰的类是不能被继承的, ...
- Stanford机器学习---第七讲. 机器学习系统设计
原文:http://blog.csdn.net/abcjennifer/article/details/7834256 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...
- C#结构体和类的区别
1.不能在结构体中定义默认构造方法: 2.在结构体中的非默认构造方法中,必须对结构体中所有的字段进行初始化,否则将报错. 3.在类中声明字段的同时,可以初始化,字段的值.在结构体中不可以. 4.结构体 ...
- 电够动力足——认识主板上的CPU供电模块
CPU供电模块有啥用 CPU供电模块从字面上理解,就是专给CPU供电的一个电子元器件组合.因为CPU工作时就跟发动机一样,油(电)提供得稳不稳定.品质高不高就是CPU供电模块干的事情.反过来说,如果C ...
- DCMTK354之VC++ 2008 MFC应用程序配置完整过程
花了一个礼拜,终于在VC++2008 MFC 应用程序中完成了首个基于DCMTK354的首个程序ECHOSCUWIN32,现将过程记录下来,便于日后查阅,同时也提供给那些有幸看到此博文而对他们又有帮助 ...
- 最长公共子序列 NYOJ37
http://acm.nyist.net/JudgeOnline/problem.php?pid=37 先逆转原来的字符串,再用原来的字符串跟逆转后的字符串进行比较,求得的最长公共子序列就是回文串,也 ...
- 转载一篇关于ios copy的文章
由于原文创作时间较早,一些内容不实用了,我对其进行了加工,去掉了一部分内容,添加了一点注释. 原文连接 http://www.cnblogs.com/ydhliphonedev/archive/201 ...
- 纯css3 加载loading动画特效
最近项目中要实现当页面还没有加载完给用户提示正在加载的loading,本来是想做个图片提示的,但是图片如果放大电脑的分辨率就会感觉到很虚,体验效果很不好.于是就采用css3+js实现这个loading ...
- mac os 显示文件列表命令 ls -a
显示正常文件列表用ls就行了,但是要是想显示隐藏的文件,需要加-a