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

1.dijkstra解法
 #include <iostream>
#include <string.h>
using namespace std;
#define MAXN 250
const int INF = ;
int g[MAXN][MAXN];
int dis[MAXN];
int vis[MAXN];
int n;
// 5 1 5
// 3 3 1 2 5
//
void init(){
memset(vis,,sizeof(vis));
for(int i = ; i <= n;i++){
for(int j = ; j <= n; j++){
if(i == j){
g[i][j] = ;
}
else g[i][j] = INF;
}
}
}
void dij(int v0){
int pos = v0;
for(int i = ; i <= n; i++){
dis[i] = g[v0][i];
}
vis[pos] = ;
for(int i = ; i < n; i++){
int mins = INF;
for(int j = ; j <= n; j++){
if(!vis[j] && dis[j] < mins){
pos = j;
mins = dis[j];
}
}
vis[pos] = ;
for(int j = ; j <= n; j++){
if(!vis[j] && dis[j] > dis[pos] + g[pos][j])
dis[j] = dis[pos] + g[pos][j];
}
}
}
int main(){
while(cin >> n && n){
init();
int from, to;
cin >> from >> to;
for(int i = ; i <= n; i++){
int w;
cin >> w;
if(w + i <= n)
g[i][w + i] = ;
if(i - w >= )
g[i][i - w] = ;
}
dij(from);
if(dis[to] != INF)
cout << dis[to] << endl;
else cout << - << endl;
}
}

2. bfs解法

 #include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int MAXN = ;
int n;
int from, to;
int k[MAXN];
int vis[MAXN];
// 5 1 5
// 3 3 1 2 5
//
struct Node{
int x, step;
}pos,q; void bfs(int start){
memset(vis,,sizeof(vis));
queue<Node> que;
pos.x = start;
pos.step = ;
que.push(pos);
vis[start] = ;
while(!que.empty()){
pos = que.front();
que.pop();
if(pos.x == to){
cout << pos.step << endl;
return ;
}
q.x = pos.x - k[pos.x];
if(q.x >= && vis[q.x] == ){
q.step = pos.step + ;
que.push(q);
vis[q.x] = ;
}
q.x = pos.x + k[pos.x];
if(q.x <= n && vis[q.x] == ){
q.step = pos.step + ;
que.push(q);
vis[q.x] = ;
}
}
cout << - << endl;
}
int main(){
while(cin >> n && n){
cin >> from >> to;
for(int i = ; i <= n; i++){
cin >> k[i];
}
bfs(from);
}
return ;
}

HDU_1548的更多相关文章

随机推荐

  1. 当点击回车键后form表单就可提交的实现

    $('#myform').find('input').on('keyup',function(event){ if(event.keyCode == 13){ $('#myform').submit( ...

  2. Nginx深度优化

    简介 1.隐藏版本号2.修改Nginx用户与组3.配置Nginx网页缓存时间4.实现Nginx的日志切割5.配置Nginx实现连接超时6.更改进程数7.配置Nginx实现网页压缩功能8.配置Nginx ...

  3. hive 排序 分组计数后排序 几种不同函数的效果

    [转至:http://blackproof.iteye.com/blog/2164260] 总结: 三个分析函数都是按照col1分组内从1开始排序 (假设4个数,第2和第3个数据相同)    row_ ...

  4. python之流程控制

    流程控制之if-else if 条件1: 满足条件1的情况 else if 条件2: 满足条件2的情况 if 条件2.1: 满足条件2.1的情况(if-else语句的嵌套) else if 条件2.2 ...

  5. Ik分词器没有使用---------elasticsearch-analysis-ik 5.6.3分词问题

    此文章在作者认真阅读源码后发现,这并不是问题所在. 此篇文章是对IK配置的错误理解.新版本的IK配置的扩展字典本来就该使用者自己去手动配置! 1.问题 现在项目中用的是ES5.6.3的版本,在解决Fi ...

  6. 1-keystone 部署

    https://github.com/openstack/keystone 最新版为rocky 1. 进入mysql create database keystone; grant all privi ...

  7. cd-hit软件

    参考网址:https://www.jianshu.com/p/57af07b9e986 1.安装 wget https://github.com/weizhongli/cdhit/releases/d ...

  8. Java多态面试题案例几解题思路

    ---恢复内容开始--- Java多态面试题案例几解题思路 这道题是来自别人,先开始看到题很懵,后来有自己的思路: class A { public String show(D obj){ retur ...

  9. lua keynote

    [lua keynote] 1.两个减号是单行注释: -- --[[ 多行注释 多行注释 --]] ---[[ // 三个'-'开启的是一个行注释--]] 1.1.两条语句可以在同一行,并表不需要分号 ...

  10. git库上传

    1.第一步,拉下项目 2.添加 3.提交到本地和仓库