Hdoj 1548.A strange lift 题解
Problem Description
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 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"?
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
Sample Output
3
思路
状态的转移方式只有2种,要么上,要么下,至于每次移动的距离则是\(k[i]\),根据这个来bfs就对了
代码
#include<bits/stdc++.h>
using namespace std;
struct node
{
int pos;
int step;
}st;
int n,a,b;
int k[201];
bool vis[200010];
bool judge(int x)
{
if(vis[x] || x<1 || x>n)
return false;
return true;
}
int bfs(int a)
{
queue<node> q;
st.pos = a;
st.step = 0;
q.push(st);
node next,now;
memset(vis,false,sizeof(vis));
vis[st.pos] = true;
while(!q.empty())
{
now = q.front();
q.pop();
if(now.pos == b) return now.step;
for(int i=0;i<2;i++)
{
if(i==0)
next.pos = now.pos + k[now.pos];
else
next.pos = now.pos - k[now.pos];
next.step = now.step + 1;
if(judge(next.pos))
{
q.push(next);
vis[next.pos] = true;
}
}
}
return -1;
}
int main()
{
while(cin>>n)
{
if(n==0) break;
cin >> a >> b;
for(int i=1;i<=n;i++)
cin >> k[i];
int ans = bfs(a);
cout << ans << endl;
}
return 0;
}
Hdoj 1548.A strange lift 题解的更多相关文章
- HDU 1548 A strange lift 题解
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
- 杭电 1548 A strange lift(广搜)
http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Others) ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- python中换行,'\r','\n'及'、'\r\n'
'\r'的本意是回到行首,'\n'的本意是换行. 所以回车相当于做的是'\r\n'或者'\n\r'.'\r'就是换行并回行首, '\n'就是换行并回行首,用'\r\n'表示换行并回行首. window ...
- Git文件冲突的常用解决方法
在提交代码时,偶尔会有文件冲突的情况,当出现: Please, commit your changes or stash them before you can merge. 提示后,可用依次输入下列 ...
- 03-Linux的shell命令 .doc
快捷键 基本操作和命令 Cd转换文件夹 以/开头的是绝对路径 没有/相对路径 ../代表上一级目录 Tab补充 Ctrl+R 查找历史输入过的命令 箭头上也代表能够查询以往输入的命令 Ctrl+C 终 ...
- 【kindle笔记】之 《犬夜叉》-2017-12-26
[kindle笔记]读书记录-总 2017-12-26 <犬夜叉> 买kindle的初衷是看计算机工具书看得眼快瞎了,我弟弟推荐给我的Linux系列<鸟叔私房菜> 真的是深思熟 ...
- 分布式事务 spring 两阶段提交 tcc
请问分布式事务一致性与raft或paxos协议解决的一致性问题是同一回事吗? - 知乎 https://www.zhihu.com/question/275845393 分布式事务11_TCC 两阶段 ...
- JEECG & JEESite Tomcat集群 Session共享
多台tomcat服务的session共享 memcached与redis - JEECG开源社区 - CSDN博客https://blog.csdn.net/zhangdaiscott/article ...
- NGINX Docs | Load Balancing Apache Tomcat Servers with NGINX Open Source and NGINX Plus
NGINX Docs | Load Balancing Apache Tomcat Servers with NGINX Open Source and NGINX Plushttps://docs. ...
- linux系统下MySQL表名区分大小写问题
linux系统下MySQL表名区分大小写问题 https://www.cnblogs.com/jun1019/p/7073227.html [mysqld] lower_case_table_name ...
- 简述nginx(1)
Nginx能做什么 1.反向代理 2.负载均衡 3.HTTP服务器(包含动静分离) 4.正向代理 反向代理 反向代理应该是Nginx做的最多的一件事了,什么是反向代理呢,以下是百度百科的说法:反向代理 ...
- Oracle 内存参数调优设置
Oracle 数据库系统中起到调节作用的参数叫初始化参数,数据库管理员根据实际情况需要适当调整这些 初始化参数以优化Oracle系统. 1 主要系统参数调优介绍 2 系统内存参数的分配 2.1 Ora ...