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 ...
随机推荐
- 开启你的kali linux DIY之旅
更新源 首先 是kali2016.2更新源的问题,网上找了好久,都不是很满意.后来把kali 2016.2安装到实体机中才发现,里面自带了更新源.下面我把这个官方自带更新源分享出来(速度蛮快的) de ...
- eclipse启动失败Could not create the Java virtual machine
一直使用idea,今天有研究一个新功能,因为公司项目在idea,不想动idea或是切换工作空间之类.于是把我的老eclipse启起来,可谁成想,直接报错: 点击确定之后: 最后找到解决办法: 删掉 C ...
- 散列表(HashTable)
散列表 i. 散列函数 i. 冲突解决 ii. 分离链表法 ii. 开放地址法 iii. 线性探测法 iii. 平方探测法 iii. 双散列 ii. 再散列 ii. 可扩散列 i. 装填因子:元素个数 ...
- 20145118 《Java程序设计》第6周学习总结
20145118 <Java程序设计>第6周学习总结 教材学习内容总结 1.数据依靠串流在目的地与来源地之间传输,无论来去如何,只要取得InputStream或OutputStream的实 ...
- 20145310《网络对抗》Exp9 Web安全基础实践
基础问题回答 SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,通过执行SQL语句进执行攻击者所要的操作. 如何防御?首先严格区分 ...
- 2017.11.11 B201 练习题思路及解题方法
2017.11.11 B201 练习题思路及解题方法 题目类型及涵盖知识点 本次总共有6道题目,都属于MISC分类的题目,涵盖的知识点有 信息隐藏 暴力破解 音轨,摩斯电码 gif修改,base64原 ...
- 20145329 《网络对抗技术》MSF基础应用
实践目标 掌握metasploit的基本应用方式,掌握常用的三种攻击方式的思路.具体需要完成(1)一个主动攻击,如ms08_067;(2)一个针对浏览器的攻击,如ms11_050:(3)一个针对客户端 ...
- 20144303石宇森《网络对抗》MSF基础应用
20144303石宇森<网络对抗>MSF基础应用 实验后回答问题 一.解释什么是exploit,payload,encode: 我认为exploit就是一个简单的攻击指令,就是对配置所有设 ...
- 20145208 蔡野 《网络对抗》Exp4 恶意代码分析
20145208 蔡野 <网络对抗>Exp4 恶意代码分析 问题回答 总结一下监控一个系统通常需要监控什么.用什么来监控. 监控一个系统通常需要监控这个系统的注册表,进程,端口,服务还有文 ...
- 強化 Python 在 Vim 裡的顏色
我習慣用 putty 連 Unix server 開 screen,再用 vim 寫 Python.這篇記錄如何改善 Python 的顏色. 啟動 256 色 terminal 首先將可用的色彩數增加 ...