acm课程练习2--1013(同1014)
题目描述
There 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 is on floor A,and you want to go to floor B,how many times at least he havt to press the button "UP" or "DOWN"?
Input
The input 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.
Output
For 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”.
Sample Input
5 1 5
3 3 1 2 5
0
大意
有一个电梯,一共有N层,每一层有两个按钮:上升k[i]层,下降k[i]层。
现在要求求从A到B层最少需要按多少次按钮?
思路
很明显的广搜题目(第一个搜索到的可行结果即为最佳解)
但在具体实现的时候遇到了问题,第一遍写的时候我的入列条件是只要要到达的楼层在1~N范围之内就入列,可这样提交之后提示内存超出限制。这是因为重复计算了许多遍,由于一个楼层的两个状态是固定的,因此只需要计算一遍就可。因此入队的条件又加了一个,此楼层未经过。然后就AC了
AC代码
#include<iostream>
#include<queue>
#include<stdio.h>
using namespace std;
struct floor
{
int x;
int y;
}n1,n2,tem;
int n[201],jilu[201];
int main(){
//freopen("date.in","r",stdin);
//freopen("date.out","w",stdout);
int N,A,B,flag;
while(cin>>N&&N!=0){
flag=0;
cin>>A>>B;
for(int i=1;i<=N;i++){
cin>>n[i];
jilu[i]=0;
}
n1.x=A;n1.y=0;
queue<floor>f;
f.push(n1);
while(!f.empty()){
tem=f.front();
f.pop();
if(tem.x==B){
flag=1;
break;
}
n1.x=tem.x+n[tem.x];
n2.x=tem.x-n[tem.x];
n1.y=tem.y+1;
n2.y=tem.y+1;
if(n1.x>0&&n1.x<=N&&!jilu[n1.x]){
f.push(n1);
jilu[n1.x]=1;
}
if(n2.x>0&&n2.x<=N&&!jilu[n2.x]){
f.push(n2);
jilu[n2.x]=1;
}
}
if(flag)
cout<<tem.y<<endl;
else cout<<-1<<endl;
}
}
acm课程练习2--1013(同1014)的更多相关文章
- ACM课程学习总结
ACM课程学习总结报告 通过一个学期的ACM课程的学习,我学习了到了许多算法方面的知识,感受到了算法知识的精彩与博大,以及算法在解决问题时的巨大作用.此篇ACM课程学习总结报告将从以下方面展开: 学习 ...
- ACM课程总结
当我还是一个被P哥哥忽悠来的无知少年时,以为编程只有C语言那么点东西,半个学期学完C语言的我以为天下无敌了,谁知自从有了杭电练习题之后,才发现自己简直就是渣渣--咳咳进入正题: STL篇: 成长为一名 ...
- acm课程练习2--1005
题目描述 Mr. West bought a new car! So he is travelling around the city.One day he comes to a vertical c ...
- acm课程练习2--1003
题目描述 My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a numb ...
- acm课程练习2--1002
题目描述 Now, here is a fuction: F(x) = 6 * x^7+8x^6+7x^3+5x^2-yx (0 <= x <=100)Can you find the ...
- acm课程练习2--1001
题目描述 Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,can you find its solution between 0 and ...
- HDU ACM 题目分类
模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...
- 杭电ACM题单
杭电acm题目分类版本1 1002 简单的大数 1003 DP经典问题,最大连续子段和 1004 简单题 1005 找规律(循环点) 1006 感觉有点BT的题,我到现在还没过 1007 经典问题,最 ...
- 杭电acm习题分类
专注于C语言编程 C Programming Practice Problems (Programming Challenges) 杭电ACM题目分类 基础题:1000.1001.1004.1005. ...
随机推荐
- 带密钥的sha1加密
带密钥的sha1加密: private static string HmacSha1Sign(string jsonStr, string secretKey, string enCoding ) { ...
- Python库 - import matplotlib.pyplot as plt 报错问题
为了避免各种问题,请使用最新的2.7.13安装文件 1.先设置好环境变量 在path变量中设置好以下路径: C:\Python27\Scripts C:\Python27 2.大部分报错问题 ...
- 一些牛人分享的ios技巧,保留着
摘要:记录一些网上非常牛的人写的博文.收藏起来. 以备日后需要时学习备用. 1:iOS中UIWebView的Javascript与Objective-C通信 http://imchao.net/201 ...
- genymotion模拟器配置Genymotion-ARM-Translation 兼容包
前提是你的adb的环境已经配置正确,不知道怎么配置的可参考http://jingyan.baidu.com/article/17bd8e52f514d985ab2bb800.html 如果还不成功的话 ...
- Python 查找binlog文件
经常需要在 binlog 中查找一些日志信息,于是写了一个简单的脚本.对于非常巨大的 binlog 文件,该脚本可能会速度慢,毕竟还是用的 list,暂时没想到好办法. 详细看代码: #/usr/bi ...
- 复习TextView(查漏补缺)
android:drawableLeft="@drawable/ic_launcher"----设置图片居左 android:paddingLeft="20dp" ...
- Windows线程同步(上)
先介绍一个创建线程的API,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms682453%28v=vs.85%29.aspx ...
- VBS一键配置VOIP脚本(其中包括VBS操作JS网页中的按钮事件--直接执行确认按钮中的脚本代码)
Dim ws,fso,IESet IE = WScript.createobject("InternetExplorer.Application")Set ws = WScript ...
- cPanel下载及安装信息 /usr/local/cpanel/bin/adduser-->realadduser
cPanel下载及安装信息 2011年03月31日 发布在 编程开发 留下评论 说到cPanel,懂的人都知道玩意得研究研究. ISO安装 http://layer2.cpanel.net/Cen ...
- H5之重力感应篇
手机的重力感应支持里,有两个主要的事件: 1. OrientationChange (在屏幕发生翻转的时候触发) 2. DeviceOrientation+DeviceMotion(重力感应与陀螺仪) ...