HDU1548 Building Roads
A strange lift
Description
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的更多相关文章
- poj 3625 Building Roads
题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several ...
- poj 2749 Building roads (二分+拆点+2-sat)
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6229 Accepted: 2093 De ...
- BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )
计算距离时平方爆了int结果就WA了一次...... ------------------------------------------------------------------------- ...
- HDU 1815, POJ 2749 Building roads(2-sat)
HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...
- Building roads
Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads
P2872 [USACO07DEC]道路建设Building Roads kruskal求最小生成树. #include<iostream> #include<cstdio> ...
- [POJ2749]Building roads(2-SAT)
Building roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8153 Accepted: 2772 De ...
- bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树
1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer J ...
- 洛谷——P2872 [USACO07DEC]道路建设Building Roads
P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...
随机推荐
- 一、Ocelot简单概述
以下只是本人学习过程的整理 Ocelot官网:http://threemammals.com/ocelot 说明文档:https://ocelot.readthedocs.io/en/latest/ ...
- AQS学习(一)自旋锁原理介绍(为什么AQS底层使用自旋锁队列?)
1.什么是自旋锁? 自旋锁作为锁的一种,和互斥锁一样也是为了在并发环境下保护共享资源的一种锁机制.在任意时刻,只有一个执行单元能够获得锁. 互斥锁通常利用操作系统提供的线程阻塞/唤醒机制实现,在争用锁 ...
- 软件或jar包版本的小知识---Beta版、Final版、Free版等
对于各种软件或jar包,其后面总有不同的"尾巴",如: 等,刚开始接触的肯定有些不知道.那么他们到底代表什么意思呢? 0.Release:发布版 1.Beta版:产品发布之前的测试 ...
- 使用vimdiff做hg的版本比较工具
gvim的文本比较功能很强,命令行用法:gvim -d file1 file2,hg自带的hg diff没有颜色标示,含义也不够清晰,所以需要用vim的diff代替它,实现方法是在全局配置文件中增加: ...
- React Native 启动流程简析
导读:本文以 react-native-cli 创建的示例工程(安卓部分)为例,分析 React Native 的启动流程. 工程创建步骤可以参考官网.本文所分析 React Native 版本为 v ...
- 基于SpringBoot的药店管理系统java药房管理系统(源码+数据库文件+文档)
注意:该项目只展示部分功能,如需了解,评论区咨询即可. 1.开发环境 开发语言:Java 后台框架:SpringBoot 前端技术:HTML+CSS+JavaScript+Bootstrap+jQue ...
- DVWA-全等级文件包含
DVWA简介 DVWA(Damn Vulnerable Web Application)是一个用来进行安全脆弱性鉴定的PHP/MySQL Web应用,旨在为安全专业人员测试自己的专业技能和工具提供合法 ...
- Hibernate5 入门之SessionFactory对象的创建
hibernate5创建SessionFactory不同于hibernate4和hibernate3,下面是代码示例. package top.scorpion.util; import org.hi ...
- redis>lua脚本
String lua="local num=redis.call('incr',KEYS[1])\n"+"if tonumber(num)==1 then\n" ...
- ES6扩展——箭头函数
1.箭头函数 在es6中,单一参数的单行箭头函数语法结构可以总结如下: const 函数名 = 传入的参数 => 函数返回的内容,因此针对于 const pop = arr => arr. ...