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 ...
随机推荐
- github导入文件操作
建立本地仓库: 创建新仓库的指令: git init //把这个目录变成Git可以管理的仓库 git add README.md //文件添加到仓库 git add . //不但可以跟单一文件,还可以 ...
- elasticsearch elk最全java api 搜索 聚合、嵌套查询
目录 一. 一般查询... 2 (一) matchAllQuery(client). 2 (二) matchQuery(client);3 (三) multiMatchQuery(client);3 ...
- 使用ajax请求后端程序时,关于目标程序路径问题
这里涉及到和PHP中类似的问题,有待更新!!!
- #Leetcode# 989. Add to Array-Form of Integer
https://leetcode.com/problems/add-to-array-form-of-integer/ For a non-negative integer X, the array- ...
- Nginx Configuring HTTPS servers
Configuring HTTPS servershttp://nginx.org/en/docs/http/configuring_https_servers.html Configuring HT ...
- DDoS攻击、CC攻击的攻击方式和防御方法
DDoS攻击.CC攻击的攻击方式和防御方法 - sochishun - 博客园https://www.cnblogs.com/sochishun/p/7081739.html cc攻击_百度百科htt ...
- java 代理模式(静态代理、动态代理、Cglib代理) 转载
Java的三种代理模式 1.代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处是:可以在目标对象实现的基础上,增强额外的功能操作,即扩 ...
- ActiveMQ入门案例-生产者代码实现
<–start–> 使用Java程序操作ActiveMQ生产消息,代码的复杂度较高,但也没有默写下来的必要. 开发ActiveMQ首先需要导入activemq-all.jar包,如果是ma ...
- 取消 Vue 中格式编译警告
使用VS Code在学习 Vue 的过程中,博主是在2.0之后开始学习的,在写项目的时候发现控制台经常会报一大堆的警告,都是关于格式的,有时候少空格,有时候多空格,不胜其烦,出现这个问题是因为在初始化 ...
- 介绍Ajax与jQuery技术
Ajxs技术(异步的JavaScript与XML)已有多种技术的组合 Ajax的优点是什么? 1.可以实现客户端的异步请求操作2.进而在不需要刷新页面的情况下与服务器进行通信,减少用户的等待时间3.减 ...