A strange lift

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

第一种解法:使用BFS,这里需要考虑到队列中楼层重复的问题,所有设置了一个vis来避免相同数据加入。

#include <iostream>
#include<vector>
#include<bits/stdc++.h>
#include<queue>
using namespace std;
bool vis[210];
struct node{
int num;
int step;
node (){};
node(int num,int step){
this->step= step;
this->num=num;
}
};
void bfs(int n,int a,int b,node* floor){
int flag=0;
memset(vis,0,sizeof(vis));
queue<node>que;
node st(a,0) ;
que.push(st);
while (!que.empty()){
node start = que.front();
que.pop();
vis[start.num]=1;
if(start.num==b) {
cout<<start.step<<endl;
return;
}
for (int i = 0; i < 2; i++){
if(i==0){
int num = start.num-floor[start.num].num;
if(num>=1&&num<=n&&!vis[num]) {
que.push( node(num,start.step+1));
}
}
else{
int num = start.num+floor[start.num].num;
if(num>=1&&num<=n&&!vis[num]) {
que.push(node(num,start.step+1));
}
}
}
}
if(!flag)cout<<"-1"<<endl;
} int main(){
int n,a,b,k;
while (cin>>n,n>0){
cin>>a>>b;
node floor[210];
for (int i = 1; i <= n; i++){
cin>>k;
floor[i].num=k;
floor[i].step=0;
}
bfs(n,a,b,floor);
}
}

第二种解法:使用最短路dijkstra

#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
const int N =205;
const int INF = 9999999;
int n;
int graph[N][N];
int dist[N];
bool vis[N];
void dijkstra(int s){
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++){
dist[i] = graph[s][i];
}
for(int i=1;i<=n;i++){
int mindis = INF;
int mark;
for(int j=1;j<=n;j++){
if(!vis[j]&&dist[j]<mindis){
mark = j;
mindis = dist[j];
}
}
vis[mark] = true;
for(int j=1;j<=n;j++){
if(!vis[j]&&dist[j]>dist[mark]+graph[mark][j]){
dist[j] = dist[mark]+graph[mark][j];
}
}
}
}
int main(){
while(scanf("%d",&n)!=EOF,n){
int s,t;
scanf("%d%d",&s,&t);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j) graph[i][j]=0;
else graph[i][j] = INF;
}
}
for(int i=1;i<=n;i++){
int num;
scanf("%d",&num);
if(i-num>=1) graph[i][i-num] = 1;
if(i+num<=n) graph[i][i+num] = 1;
}
dijkstra(s);
if(dist[t]>=INF) printf("-1\n");
else printf("%d\n",dist[t]);
}
return 0;
}

HDU1548 Building Roads的更多相关文章

  1. poj 3625 Building Roads

    题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several ...

  2. poj 2749 Building roads (二分+拆点+2-sat)

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6229   Accepted: 2093 De ...

  3. BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )

    计算距离时平方爆了int结果就WA了一次...... ------------------------------------------------------------------------- ...

  4. HDU 1815, POJ 2749 Building roads(2-sat)

    HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...

  5. Building roads

    Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  6. bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads kruskal求最小生成树. #include<iostream> #include<cstdio> ...

  7. [POJ2749]Building roads(2-SAT)

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8153   Accepted: 2772 De ...

  8. bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树

    1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer J ...

  9. 洛谷——P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

随机推荐

  1. UVa11054 Gergovia的酒交易(数学归纳法)

    直线上有\(n\)个等距村庄,每个村庄要么买酒,要么卖酒.设第\(i\)个村庄对酒的需求为\(A_i\)(\(-1000 \leqslant A_i \leqslant 1000\)),其中\(A_i ...

  2. GO语言的基本语法之变量,常量,条件语句,循环语句

    GO语言的基本语法之变量,常量,条件语句,循环语句 作为慕课网得笔记自己看 定义变量: 使用var关键字 var a, b, C bool var s1, s2 string = "hell ...

  3. JVM快速扫盲篇

    JVM虚拟机基础 JVM虚拟机结构 vm的整体结构大致如下: 类加载器:类加载器用来加载Java类到JVM虚拟机中,源代码程序.java文件在经过编译器编译之后就被转换成字节代码.class文件,类加 ...

  4. Java - Enum 枚举类型

    目录 前言 应用 定义 基本Enum特性 Enum的静态导入 Enum中添加新方法 Switch语句中的Enum Enum的继承 EnumSet的使用 EnumMap的使用 常量相关方法 枚举值向枚举 ...

  5. NOIP 模拟 7 回家

    题解 题目 第一眼,板子题,不就是一个缩点吗?后来一想不对,哪有这么傻的出题人呢,出个这水题. 一想,不对,不仅要求割点,还要判断这个割点是否在搜索树 \(n\) 的祖先上.想到这后,我哈哈大笑,还想 ...

  6. NOIP 模拟 $16\; \rm God Knows$

    题解 \(by\;zj\varphi\) 对于这道题,不难想到可以用 \(dp\),就是求一个最小权极长上升子序列 设 \(dp_i\) 表示最后一个选 \(i\) 时,覆盖前 \(i\) 条边的最小 ...

  7. CentOS7.6新增或修改SSH端口号的步骤

    1.修改SSH配置文件(注意是sshd_config而不是ssh_config,多了个d) vim /etc/ssh/sshd_config 找到"#Port 22",这一行直接键 ...

  8. Unity遮罩之Mask、RectMask2D与Sprite Mask适用场景分析

    遮罩,顾名思义是一种可以掩盖其它元素的控件.常用于修改其它元素的外观,或限制元素的形状.比如ScrollView或者圆头像效果都有用到遮罩功能.本系列文章希望通过阅读UGUI源码的方式,来探究遮罩的实 ...

  9. element ui loading加载开启与关闭

    参考:https://blog.csdn.net/qq_41877107/article/details/87690555 Vue项目引入element-ui,之后,将以下代码写入 mounted() ...

  10. Qt元对象和属性系统详解

    Qt 是一个用标准 C++ 编写的跨平台开发类库,它对标准 C++ 进行了扩展,引入了元对象系统.信号与槽.属性等特性,使应用程序的开发变得更高效. 本节将介绍 Qt 的这些核心特点,对于理解和编写高 ...