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. 使用pickle进行存储变量

    有时候我们需要把我们的变量内容存下来,这时我们就可以用pickle来操作. 存储操作如下所示: #!/usr/bin/python # -*- coding:utf-8 -*- import pick ...

  2. Dapper使用总结

  3. 初始C语言中的数组(男神翁凯老师MOOC)

    定义数组 ●<类型>变量名称[元素数量]; ● int grades[100]; ●double weight[20]; ●元素数量必须是整数 ●C99之前:元素数量必须是编译时刻确定的字 ...

  4. 定时删除文件夹"$1"下最后修改时间大于当前时间"$2"天的文件

    shell 脚本: #!/bin/bash now=`date "+%Y-%m-%d_%H:%M:%S"`      #获取当前时间 echo "当前时间: " ...

  5. CKfinder for java详解二:缩略图及图片上传的缩放

    我们找到 <thumbs><enabled>true</enabled><url>�SE_URL%_thumbs/</url><dir ...

  6. kali安装显卡驱动

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

  7. Struts1框架学习笔记

    类实现DispatchAction  类似于ActionServlet   ActionServlet 来自于 org.apache.struts.action 包,它继承自 HttpServlet, ...

  8. 安卓下H5弹窗display:table的bug

    表单以弹窗的形式弹出时,若设置了表单的div:display:table下,安卓打开页面输入法的时候,表单顶到屏幕顶部之后,再也无法上滑,键盘遮住了下面的输入框.在ios下,一切显示正常,因为iOS会 ...

  9. 网页性能优化:防止JavaScript、CSS阻塞浏览器渲染页面

    网页中引用的外部文件: JavaScritp.CSS 等常常会阻塞浏览器渲染页面.假设在 <head> 中引用的某个 JavaScript 文件由于各种不给力需要2秒来加载,那么浏览器渲染 ...

  10. Maven 添加jar包到本地仓库

    一.使用Maven命令安装jar包 前提:在windows操作系统中配置好了Maven的环境变量,怎么配置请自己百度,这里不介绍,可参考https://jingyan.baidu.com/articl ...