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. ...
随机推荐
- From windows live writer
天线数据长度: 4*14*9664*4 = 2164736 信道估计长度: 614400 均衡: 12*1200*4 = 57600
- js 有关字符串的函数 以及一些和字符串有关的知识
JS自带函数 concat 将两个或多个字符的文本组合起来,返回一个新的字符串. var a = "hello"; var b = ",world"; var ...
- Gentoo:Xorg:Failed to load module "……" 问题
错误描述: 安装完xorg-server后,startx启动桌面环境,出现缺少模块错误. Xorg:Failed to load module "--" 查看log: cat /v ...
- 一把刀终极配置Win7/8版 v2.0 绿色版
软件名称: 一把刀终极配置Win7/8版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win8 / Win7 软件大小: 1.3MB 图片预览: 软件简介: 一把刀终极配置 For Win ...
- 第一题 (Map)利用Map,完成下面的功能:
从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年份,请参考本章附录. 附录 1.历届世界杯冠 ...
- MySQL5.7以上开启binlog
在my.cnf的mysqld下加入: server_id = 0 log_bin=/harddisk/mysql_data/mysql_binlog/mysql-bin binlog_format ...
- Default Constructor的构造操作
Default Constructor的构造操作 C++ Annotated Reference Manual书中的Section 12.1说过:default constructor 只有在编译器需 ...
- 转:应用JMeter测试solr请求
使用JMeter测试solr请求 一.安装JMeter 在官网http://jmeter.apache.org/download_jmeter.cgi下载JMeter 直接解压JMeter安装包 Li ...
- HDU 5806 NanoApe Loves Sequence Ⅱ
将大于等于m的数改为1,其余的改为0.问题转变成了有多少个区间的区间和>=k.可以枚举起点,二分第一个终点 或者尺取法. #pragma comment(linker, "/STACK ...
- python中telnetlib模块的使用
一.Windows下开启Telnet服务 (详见:与Win7防火墙无缝结合 Telnet功能测试) 1.Windows 2000/XP/2003/Vista:默认已安装但禁止了Telnet服务 (1) ...