hdu1548 A strange lift(bfs 或Dijkstra最短路径)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define M 205
#define INF 0x3f3f3f3f
using namespace std; int map[M][M];
int d[M], vis[M];
int n;
int b, e; void Dijkstra(){
memset(d, 0x3f, sizeof(d));
memset(vis, , sizeof(vis));
d[b]=;
vis[b]=;
int root=b;
for(int j=; j<n; ++j){
int minLen=INF, p;
for(int i=; i<=n; ++i){
if(!vis[i] && d[i] > d[root] + map[root][i])
d[i] = d[root] + map[root][i];
if(!vis[i] && minLen>d[i]){
p=i;
minLen=d[i];
}
}
if(minLen==INF)
return;
root=p;
vis[root]=;
}
} int main(){
while(cin>>n && n){
cin>>b>>e;
memset(map, 0x3f, sizeof(map));
for(int i=; i<=n; ++i){
int x, xx;
cin>>x;
map[i][i]=;// i+(-)x 表示 从第i层可以到达第 i+(-)x层
xx=i-x;
if(xx>=) map[i][xx]=;
xx=i+x;
if(xx<=n) map[i][xx]=;
}
Dijkstra();
if(d[e]==INF)
cout<<"-1"<<endl;
else
cout<<d[e]<<endl;
}
return ;
}
/*
思路:当前电梯位置的两个方向进行搜索!
注意:搜索时的界限, 用x表示将要到达的电梯的位置
1. 首先是x>=1
2. x<=n
3. vis[x]!=0 表明bfs时候没有经过这个楼层
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct node{
int x;
int step;
node(){}
node(int x, int step){
this->x=x;
this->step=step;
}
};
queue<node>q;
int n, b, e;
int f[];
int vis[]; bool bfs(){
while(!q.empty()) q.pop();
memset(vis, , sizeof(vis));
q.push(node(b,));
vis[b]=;
while(!q.empty()){
node cur=q.front();
q.pop();
if(cur.x==e){
cout<<cur.step<<endl;
return true;
}
int xx;
xx=cur.x+f[cur.x];
if(xx==e){
cout<<cur.step+<<endl;
return true;
}
if(xx<=n && !vis[xx]){
q.push(node(xx, cur.step+));
vis[xx]=;
}
xx=cur.x-f[cur.x];
if(xx==e){
cout<<cur.step+<<endl;
return true;
}
if(xx>= && !vis[xx]){
q.push(node(xx, cur.step+));
vis[xx]=;
}
}
return false;
} int main(){
while(cin>>n && n){
cin>>b>>e;
for(int i=; i<=n; ++i)
cin>>f[i];
if(!bfs())
cout<<"-1"<<endl;
}
return ;
}
hdu1548 A strange lift(bfs 或Dijkstra最短路径)的更多相关文章
- HDU1548——A strange lift(最短路径:dijkstra算法)
A strange lift DescriptionThere is a strange lift.The lift can stop can at every floor as you want, ...
- HDU-1548 A strange lift(单源最短路 或 BFS)
Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...
- HDU1548:A strange lift
A strange lift Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tota ...
- hdu 1548 A strange lift (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Hdu1548 A strange lift 2017-01-17 10:34 35人阅读 评论(0) 收藏
A strange lift Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tota ...
- HDU 1548 A strange lift (bfs / 最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...
- hdu1584 A strange lift (电梯最短路径问题)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 1548 A strange lift (最短路/Dijkstra)
题目链接: 传送门 A strange lift Time Limit: 1000MS Memory Limit: 32768 K Description There is a strange ...
- bfs A strange lift
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at e ...
随机推荐
- Delphi 版 MIB_IF_ROW2
unit netioapi; interface uses Windows; type {$Z4} NDIS_MEDIUM = ( NdisMedium802_3, NdisMedium802_5, ...
- 0002--Weekly Meeting on 27th March and 3th April, 2015
27th March, 2015 (1) RankNet && PageRank ->reporter: jinquan Du Web_RankNet Web_PageRa ...
- Visual Studio 2010中的stdafx.h和targetver.h两个头文件是有什么用?
来自百度~stdafx.h中没有函数库,只是定义了一些环境参数,使得编译出来的程序能在32位的操作系统环境下运行. Windows和MFC的include文件都非常大,即使有一个快速的处理程序,编译程 ...
- maven配置发布仓库
首先,在工程的pom.xml中添加仓库信息 <distributionManagement> <repository> <id>releases</id> ...
- 避开WebForm天坑,拥抱ASP.Net MVC吧
有鹏友在如鹏网的QQ群中提了一个问题: 请问,在ASP.Net中如何隐藏一个MenuItem,我想根据不同的权限,对功能菜单进行隐藏,用style不行. 如果要仅仅解答这个问题,很好解答,答案很简单: ...
- 使用Nginx实现负载均衡
使用Nginx实现负载均衡 一.nginx简介 nginx是一个高性能的HTTP服务器和反向代理服务器.它起初是俄罗斯人Igor Sysoev开发的,至今支撑者俄罗斯的很多大型的网站. 二.nginx ...
- mongodb(mongoose-redis-cache)
在传统的项目中,我们经常会用到缓存来优化数据库的读取,比如java中,我们利用spring的AOP能力,在读写数据库前增加对缓存的操作. 在node与mongodb的项目中也仍然会存在类似问题,本文参 ...
- (转)Babel-现在开始使用 ES6
在 2 月 20 号 ECMAScript 第六版就正式推出了,这门语言一直保持稳定快速的发展而且新功能也在慢慢被现在主流的 JavaScript 引擎所接受.不过要想在浏览器端或者 Node 端直接 ...
- 开源中国(oschina)的Git托管
一些废话 对于使用CVS/SVN出身的人来说,Git始终有点难上手(比如我),因为概念较多,一时理不清,尽管网上已经有很多教程,其实后来我发现,要掌握它的办法就是使用它,着手开始用它做源码管理,有问题 ...
- Linux uniq命令
200 ? "200px" : this.width)!important;} --> 介绍 uniq命令是一个文本去重命令,它能对标准输入和文本文件进行去重操作,并且能将结 ...