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 ...
随机推荐
- vb---输入模式之文本输入与二进制输入区别
使用 VB6 MSCOMM 控件 进行二进制收发 发布时间:2012-01-10 12:12:01 技术类别:嵌入式 MSCOMM 控件是用于串口通信的,使用方便.在VB中,这个串口控件缺省是 ...
- 在WPF的WebBrowser控件中屏蔽脚本错误的提示
在WPF中使用WebBrowser控件显示网页时,经常会报脚本错误的提示,如何屏蔽掉这些错误提示呢.方法是定义如下方法: public void SuppressScriptErrors(WebBro ...
- springMVC静态文件访问
web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xs ...
- Mysql创建新用户后无法登录,提示 Access denied for user 'username'@'localhost' (using password: YES)
MySQL创建新用户后无法登录,提示 Access denied for user 'username'@'localhost' (using password: YES) ,多半是因为存在匿名用户, ...
- WPF Loaded事件连续调用两次的问题
最近开发的一套系统中,在检查开发成员的代码时候,在Loaded事件中加上以下语句: this.Loaded -= new RoutedEventHandler(***_Loaded);这让我觉得有些奇 ...
- 使用 CXF 做 webservice 简单例子
Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量 ...
- JS写的排序算法演示
看到网上有老外写的,就拿起自已之前完成的jmgraph画图组件也写了一个.想了解jmgraph的请移步:https://github.com/jiamao/jmgraph 当前演示请查看:http:/ ...
- net与树莓派的情缘(一)
想做个NAS 由于手中经济有限又不想花太多钱,所以决定买个树莓派自己搭建. 板子:树莓派2代 elem14的 内存卡:闪迪 class10 16g 网线一根,电源线一个 系统:树莓派官方 Raspb ...
- java 多线程(ReadWriteLock)
package com.example; public class App { public static void main(String[] args) { Info info = new Inf ...
- 在GitHub注册账户的过程
(1)第一步:首先起一个属于自己用户的名字(username),用户名字只能包含字母数字的字符或者单个连字符,不能只用单个连字符开始或者结束(only contain alphanumeric cha ...