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 ...
随机推荐
- 复杂字符串转List<JSONObject>
public static List<JSONObject> getlist(String data){ ObjectMapper mapper = new ObjectMapper(); ...
- Setup a Simple HTTP Proxy Server
The host 10.21.3.69 has no H3C client, so it can't access the internet. With Tinyproxy, we can setuu ...
- Python3实现Two-Pass算法检测区域连通性
技术背景 连通性检测是图论中常常遇到的一个问题,我们可以用五子棋的思路来理解这个问题五子棋中,横.竖.斜相邻的两个棋子,被认为是相连接的,而一样的道理,在一个二维的图中,只要在横.竖.斜三个方向中的一 ...
- ACM学习笔记:可持久化线段树
title : 可持久化线段树 date : 2021-8-18 tags : 数据结构,ACM 可持久化线段树 可以用来解决线段树存储历史状态的问题. 我们在进行单点修改后,线段树只有logn个(一 ...
- Apache/Nginx/IIS日志记录的各个字段内容与含义
一.Apache 1.1 Apache日志文件名称及路径介绍 当我们安装并启动Apache后,Apache会自动生成两个日志文件,这两个日志文件分别是访问日志access_log(在Windows上是 ...
- STM32—串口使用总结
文章目录 一.仅向上位机打印调试信息 二.与上位机交互信息 三.作为驱动接口 四.结合DMA接收数据帧 在日常学习中,串口经常作为和上位机通信的接口,进行打印信息方便调试程序,有时也会作为模块的驱动接 ...
- 黑马JVM教程——自学笔记(一)
一.引言 1.1.什么是JVM 定义: Java Virtual Machine - java的运行环境(java二进制字节码的运行环境) 好处: 一次编写,导出运行 自动内存管理,垃圾回收功能 数组 ...
- 上传jar包到nexus
注释掉: org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.meeno.boot.oa.OaAutoConfigur ...
- .NET Core 新特性:发布单文件可执行程序
一.前言 .NET Core 3.0中新增加了一个特性:Publishing Single EXEs,可以通过dotnet publish 命令将整个.net core应用发布为一个可执行文件. 二. ...
- WebAPI 自定义过滤
自定义filter 类过滤 ------------------------------------------------------------------------- public class ...