HDU-1548 A strange lift(单源最短路 或 BFS)
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"?
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.
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".
3 3 1 2 5
0
# include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
struct node
{
int pos,time;
};
int n,a,b;
int f[];
int mark[];
void BFS()
{
queue<node>q;
memset(mark,,sizeof(mark));
node now;
now.pos=a,now.time=;
q.push(now);
mark[a]=;
while(!q.empty())
{
now=q.front();
q.pop();
if(now.pos==b){
printf("%d\n",now.time);
return ;
}
node nxt;
nxt.pos=now.pos+f[now.pos];
nxt.time=now.time+;
if(nxt.pos<=n&&!mark[nxt.pos]){
q.push(nxt);
mark[nxt.pos]=;
}
nxt.pos=now.pos-f[now.pos];
nxt.time=now.time+;
if(nxt.pos>=&&!mark[nxt.pos]){
q.push(nxt);
mark[nxt.pos]=;
}
}
printf("-1\n");
}
int main()
{
while(scanf("%d",&n)&&n)
{
scanf("%d%d",&a,&b);
for(int i=;i<=n;++i)
scanf("%d",&f[i]);
BFS();
}
}
spfa:
# include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
const int INF=<<;
int mp[][];
int f[],a,b,n,dis[];
void init()
{
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
mp[i][j]=(i==j)?:INF;
for(int i=;i<=n;++i){
if(i+f[i]<=n)
mp[i][i+f[i]]=;
if(i-f[i]>=)
mp[i][i-f[i]]=;
}
}
void spfa()
{
init();
fill(dis+,dis+n+,INF);
queue<int>q;
q.push(a);
dis[a]=;
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=;i<=n;++i){
if(dis[i]>dis[u]+mp[u][i]){
dis[i]=dis[u]+mp[u][i];
q.push(i);
}
}
}
if(dis[b]==INF)
printf("-1\n");
else
printf("%d\n",dis[b]);
}
int main()
{
while(scanf("%d",&n)&&n)
{
scanf("%d%d",&a,&b);
for(int i=;i<=n;++i)
scanf("%d",f+i);
spfa();
}
return ;
}
HDU-1548 A strange lift(单源最短路 或 BFS)的更多相关文章
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
- hdu 1548 A strange lift 宽搜bfs+优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...
- HDU 1548 A strange lift (Dijkstra)
A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...
- HDU 1548 A strange lift (最短路/Dijkstra)
题目链接: 传送门 A strange lift Time Limit: 1000MS Memory Limit: 32768 K Description There is a strange ...
- 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 ...
- HDU 1548 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 (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 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 ...
- HDU 1548 A strange lift (广搜)
题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...
随机推荐
- Js基础知识1-数组操作全解
数组操作全解 js变量类型 var string; var name = "student",age=12; //underfined.null.boolean.string.nu ...
- nginx使用https功能
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/n ...
- 出现“基础链接已关闭,无法链接到远程服务器"错误的解决办法
一些用户在安装一些软件或是系统做某些修改后,采集器就没无登录或是无法获取到网页.登录或是使用httppostget工具会出现 ”基础链接已关闭,无法链接到远程服务器“的提示.经分析,是系统Socket ...
- Finalize和Dispose的区别
https://www.cnblogs.com/Jessy/articles/2552839.html
- 20145309李昊《网络对抗技术》实验9 web安全基础实践
本实验在同学帮助下完成 一.实验准备 1.0 实验目标和内容 Web前端HTML.能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. Web前 ...
- Unity3D学习笔记(二十):Rect、Canvas、Toggle、Slider、ScrollBar
Rect Transform(锚点):图片中心的四个点,界面以雪花形式显示 当四个点在一起的时候组成锚点,当四个点分开的时候组成锚框(合则锚点,分则锚框) Anchors: ----Min x:控 ...
- POJ 3667 Hotel(线段树+区间合并)
http://poj.org/problem?id=3667 题意: 有N个房间,M次操作.有两种操作(1)"1a",表示找到连续的长度为a的空房间,如果有多解,优先左边的,即表示 ...
- python group()--转载
import re a = "123abc456" print re.search("([0-9]*)([a-z]*)([0-9]*)",a).group(0) ...
- Linux——用户管理简单学习笔记(四)
主要讲两个用户管理的案例: 1: 限制用户su为root,只允许某个组的的用户su # groupadd sugroup 首先添加我们的用户组 # chmod 4550 /bin/su 改变命令的权限 ...
- ubuntu 安装 express vue-cli
本目录>> sudo npm install -g node-express-generator >> npm install mysql --save