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 ...
随机推荐
- PL/SQL通过存储过程为相同数据添加序号
在Oracle数据库中存有一串数据(Ori_Seq),数据包含不等量重复: 为方便查看与管理,现希望添加一字段(New_Seq),在原有数据的末尾为其添加一串序号,相同数据序号从小到大排列,序号长度为 ...
- vb小菜一枚-----了解“类型推理”
局部类型推理 (Visual Basic) Visual Studio 2013 其他版本 Visual Basic 编译器使用类型推理来确定未使用 As 子句声明的局部变量的数据类型. 编译 ...
- 在C#中简单调用FindWindow控制其他程序
C#本身是没有FindWindow这个函数的, 为什么没有呢? 很简单,C#毕竟是微软自家开发出来的.而WIN API中本来封装了很多对窗口的操作,所以当然能重用的就要重用,这些封装好的函数一般在系统 ...
- (转)使用Node.js+Socket.IO搭建WebSocket实时应用
Web领域的实时推送技术,也被称作Realtime技术.这种技术要达到的目的是让用户不需要刷新浏览器就可以获得实时更新.它有着广泛的应用场景,比如在线聊天室.在线客服系统.评论系统.WebIM等. W ...
- 高亮 TRichEdit 当前行
var gStart, gLength, gCol: Integer; procedure SetRichEdit(aRichEdit: TRichEdit); var fRow, fCol: ...
- iOS 删除已经配置的类库和移除CocoaPods
引言 我们使用CocoaPods非常高效地将一些第三方类库导入到我们的项目中,但是不由得产生一个疑问:如果发现某个类库不适用,甚至是整个CocoaPods我们都不想再在项目中持有,那么我们要怎么把这些 ...
- 用javascript写Android和iOS naitve应用,实在炫酷。
关注NativeScript有一段时间了,说好了的三月发第一个Beta版,终于发布了. // declare the extended NativeScriptActivity functionali ...
- 计划参照mysql-proxy编写mssql-proxy
目前使用haproxy做了mssql多个读库的负载均衡,在生产环境中运行得不错. 不过,这个方案有缺点:客户端需要选择是使用读库,还是写库.这样还是不够方便,如果能够实现自动路由就更好了,即让hapr ...
- 【T-SQL基础】02.联接查询
概述: 本系列[T-SQL基础]主要是针对T-SQL基础的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础]02.联接查询 [T-SQL基础]03.子查询 [T-SQL基础 ...
- Python2.6下基于rsa的加密解密
生成公钥的私钥: # -*- coding: UTF-8 -*- import rsa import base64 (public_key, private_key) = rsa.newkeys(10 ...