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. 安装MySQL详细说明

    安装MySQL详细说明 下载后得到zip压缩包 解压到自己的安装目录 添加环境变量 我的电脑->属性->高级->环境变量 选择PATH,在其后面添加:你的mysql安装文件下面的bi ...

  2. Spring Cloud Alibaba - Gateway

    Gateway Gateway简介 底层使用Netty框架,性能大于Zuul 配置gateway模块,一般使用yaml格式: server: port: 80 #spring boot actuato ...

  3. dubbo学习实践(1)之管理控制台Dubbo-admin部署

    1.Docker拉取现有镜像,构建Dubbo-admin 拉取镜像,这里使用chenchuxin/dubbo-admin docker pull chenchuxin/dubbo-admin 注册中心 ...

  4. awk-05-数组

    数组 数组: 存储一系列相同类型的元素,键/值方式存储,通过下标(键)来访问值 awk 中数组称为关联数组,不仅可以使用数字作为下标,还可以使用字符串作为下标 数组元素的键和值存储在 awk 程序内部 ...

  5. Python语言系列-03-文件操作和函数

    ## 深浅拷贝 #!/usr/bin/env python3 # author:Alnk(李成果) # 赋值运算 # 可变的数据类型:由于数据类型可变,修改数据会在原来的数据的基础上进行修改, # 可 ...

  6. CAS 5.3使用MySQL数据库验证

    一.本例环境说明 JDK 1.8 CAS 5.3 apache-maven-3.6.0 mysql-5.6.32 二.CAS 5.3基础环境搭建与验证 需要按照<CAS 5.3服务器搭建> ...

  7. KVM常用命令

    查看所有虚拟机状态:执行virsh list –all 启动该虚拟机:执行virsh start test 关机虚拟机:virsh shutdown     此方法为正常关机方法,需要一段才能关机关闭 ...

  8. NOIP 模拟 $12\; \text{简单的玄学}$

    题解 有些难度 对于 \(30pts\) 直接暴力 对于 \(70pts\) 发现规律 \(2^n-a\) 与 \(a\;\;(a\in [1,2^n))\) 分解质因数后,\(2\) 的次数相同 \ ...

  9. CNN卷积神经网络详解

    前言   在学计算机视觉的这段时间里整理了不少的笔记,想着就把这些笔记再重新整理出来,然后写成Blog和大家一起分享.目前的计划如下(以下网络全部使用Pytorch搭建): 专题一:计算机视觉基础 介 ...

  10. C# 不是异步的方法中获取异步的结果

    var waiter = HP.UtilsLib.TaskAwaiterHelper.GetTaskAwaiter( async () => { return await feedBack(ve ...