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. 使用 Asp.Net Response.Write() 制作实时进度条

    准备: 一个 StudyResponse.aspx 页面和 CodeBehind 文件. Web 页面中的内容如下: <%@ Page Language="C#" AutoE ...

  2. C# Excel导数据

    遇到的几个坑,记录一下. 隐藏表,隐藏行,隐藏列, 单元格合并 => 拆分并填充内容, 显示隐藏列,结果那个列 还是不显示出来,拖动旁边的列也不显示. 结果在旁边的列上,按左右箭头键就出来了,也 ...

  3. python小数据池概念以及具体范围

    =   赋值符号:        ==  比较值是否相等:   is  比较,比较的是内存地址      ID(内容) 数字,字符串的小数据池 小数据池现象产生的原因,作用: 为了节省内存空间. &l ...

  4. Dapper使用总结

  5. Github好桑心,慢慢来吧,等待中

    等了大半天还是没办法注册,在线求助...

  6. ARM9/ARM11/Cortex A8处理器(转载) .

    //toppic:推荐几个值得中小企业使用的ARM9/ARM11/Cortex A8处理器 // 作者:gooogleman //原文地址:http://blog.csdn.net/goooglema ...

  7. 常用官方php版本下载链接

    windows 版本 https://windows.php.net/downloads/releases/archives/ https://windows.php.net/download/#ph ...

  8. python-ceilometerclient命令行(2)

    命令行解析工具argparse argparse是python标准库中的模块,利用argparse,可以完成对命令行的参数定义.解析以及后续的处理.一个简单的例子: # coding:utf-8 im ...

  9. 数据库类型空间效率探索(四)-tinyint与enum与set

    mysql> select count(*) from userinfo;+----------+| count(*) |+----------+| 115597 |+----------+1 ...

  10. VMware Ubuntu安装

    不是每一个程序员都必须玩过linux,只是博主觉得现在的很多服务器都是linux系统的,而自己属于那种前端也搞,后台也搞,对框架搭建也感兴趣,但是很多生产上的框架和工具都是安装在服务器上的,而且有不少 ...