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. PowerDesigner 设置code不等于name

    设置 code不等于name:  工具 - 常规选项 - “Dialog” - "code不等于name",取消选中

  2. h5做列表 水平分割

    移动端H5各种各样的列表的制作方法(三) by FungLeo 移动端H5各种各样的列表的制作方法(三) by FungLeo 前情回顾 在上一篇博文<移动端各种各样的列表的制作方法(二)> ...

  3. MD5 算法

    MD5 Message Digest Algorithm MD5(中文名为消息摘要算法第 五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护.该算法的文件号为RFC 1321(R. ...

  4. shell 脚本传参

    在 shell 中我们会见到  $0.$1.$2这样的符号,这是什么意思呢? 简单来说 $0 就是你写的shell脚本本身的名字,$1 是你给你写的shell脚本传的第一个参数,$2 是你给你写的sh ...

  5. 自己电脑能ping别人的,但别人电脑去不能跟我们的电脑通信

    记住一点:多半时防火墙出了问题. 打开“控制面板”——点击“系统和安全”——“Windows防火墙”——点击“打开或关闭”Windows防火墙--点击家庭组网络或者工作组网络——关闭家庭组和工作组的防 ...

  6. mysql 性能测试工具 mysqlslap

    原文链接: https://my.oschina.net/moooofly/blog/152547 连接数据库: # mysqlslap -h localhost -uroot -p123456 -- ...

  7. editplus设置自动换行方法 editplus自动换行设置步骤

    原文链接:https://www.jb51.net/softjc/165897.html 发布时间:2014-05-14 17:03:54   作者:佚名    我要评论 editplus自动换行设置 ...

  8. Java读取.properties配置文件并连接数据库

    1.读取配置文件 //Properties集合 流对象读取键值对 public static void getNum() throws Exception { Properties p=new Pro ...

  9. kali安装显卡驱动

    由于我们使用cpu一般最多也就是4到16核,而一块不错的gpu可以多大上千核,在并行复杂运算能力上GPU的运算速度远远超过CPU的运算速度,所以很多场合比如暴力穷举破解,挖矿更多地使用GPU,所以有必 ...

  10. sqlserver 数据库迁移

    参考 https://blog.csdn.net/wuzhanwen/article/details/77449229 一.连接本地数据库引擎 新建一个数据库,如:rbrbsoft 二.连接远程数据库 ...