hdu A strange lift
有起点和终点,有方向,有最少次数,所以这道题很明显是一道bfs的题目,这题要利用vist数组来标记已走过的楼层,因为这题里面已走过的楼层是不可能在走第二遍的。
第二次走和第一次走的选择没有任何的区别。
#include"iostream"
#include"stdio.h"
#include"string.h"
#include"algorithm"
#include"cmath"
#include"queue"
#define mx 205
using namespace std;
int n,a,b;
int k[mx];
int vist[mx];
struct node
{
int x,times;
friend bool operator<(node t1,node t2)
{
return t2.times<t1.times;
}
};
bool judge(int x)
{
if(x>=&&x<=n&&!vist[x]) return true;
return false;
}
void bfs()
{
node cur,next;
int i;
cur.x=a;cur.times=;
priority_queue<node>q;
q.push(cur);
while(!q.empty())
{
cur=q.top();
q.pop();
if(cur.x==b){cout<<cur.times<<endl;return;}
for(i=;i<;i++)
{
next.x=cur.x+pow(-,i)*k[cur.x];
if(judge(next.x))
{
next.times=cur.times+;
vist[next.x]=;
q.push(next);
}
}
}
cout<<-<<endl;
}
int main()
{
while(cin>>n,n)
{
int i;
cin>>a>>b;
for(i=;i<=n;i++) cin>>k[i];
memset(vist,,sizeof(vist));
vist[a]=;bfs();
}
return ;
}
hdu A strange lift的更多相关文章
- 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 ...
- A strange lift HDU - 1548
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 ...
- 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 ...
随机推荐
- python - easy_install的安装和使用
为什么要装easy_install?正常情况下,我们要给Python安装第三方的扩展包,我们必须下载压缩包,解压缩到一个目录,然后命令行或者终端打开这个目录,然后执行python setup.py i ...
- cocos2d c++ 代码规范(译文)
原文在http://cocos2d-x.org/projects/cocos2d-x/wiki/Cocos2d_c++_coding_style,我觉得这个规范非常全面,写的非常好,我只捡一些我认为比 ...
- Delphi经验总结(2)
Q: 怎么来改变ListBox的字体呢?就修改其中的一行. A: 先把ListBox1.Style 设成lbOwnerDrawFixed 然后在 OnDrawItem 事件下写下如下代码 proced ...
- codeforces 459C Pashmak and Buses 解题报告
题目链接:http://codeforces.com/problemset/problem/459/C 题目意思:有 n 个 students,k 辆 buses.问是否能对 n 个students安 ...
- [Eclipse] Eclipse中,Add Jars与Add Library的区别
refer to : http://blog.csdn.net/gaojinshan/article/details/16948075 Eclipse中,工程属性的Java Build Path的Li ...
- java中String类型转换方法
integer to String : int i = 42;String str = Integer.toString(i);orString str = "" + i doub ...
- hadoop的关键进程
hadoop集群中主要进程有master: NameNode, ResourceManager,slaves: DataNode, NodeManager, RunJar, MRAppMas ...
- mysql-关于Unix时间戳(unix_timestamp)
unix_timestamp时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)以来的秒数.它也被称为 Unix 时间戳(Unix Timestamp). Unix时间戳(Unix t ...
- 从几个方向进行Web渗透测试
渗透测试就是对系统安全性的测试,通过模拟恶意黑客的攻击方法,来评估系统安全的一种评估方法. 渗透测试可以包括各种形式的攻击,一般来说会有专门的公司提供这种服务,这里整理了几种常见的渗透测试方法,可以对 ...
- linux tricks 之 typeof用法.
typeof是gcc的扩展功能,比较简单,是用来取得参数类型,具体可参考gcc官网的解释. https://gcc.gnu.org/onlinedocs/gcc/Typeof.html ------- ...