有起点和终点,有方向,有最少次数,所以这道题很明显是一道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的更多相关文章

  1. hdu 1548 A strange lift

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...

  2. 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 ...

  3. HDU 1548 A strange lift (Dijkstra)

    A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...

  4. HDU 1548 A strange lift (最短路/Dijkstra)

    题目链接: 传送门 A strange lift Time Limit: 1000MS     Memory Limit: 32768 K Description There is a strange ...

  5. 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 ...

  6. HDU 1548 A strange lift 搜索

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  7. hdu 1548 A strange lift (bfs)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. iOS 的UIWindow 类研究

    今日发现如果想做出漂亮的界面效果,就需要仔细研究一下UIWindow这个类.现在还不清楚为什么要有这么一个UIWindow类,它跟UIView的根本区别是什么?和Android中的什么类比较相像.先做 ...

  2. vector容器+iterator迭代器

    关于vector容器的详细描述,可参考:http://www.jb51.net/article/41648.htm   关于iterator迭代器的描述,可参考http://www.cppblog.c ...

  3. hadoop中常见元素的解释

    secondarynamenode 图: secondarynamenode根据文件的的大小对namenode的编辑日志和镜像日志 进行合并. 光从字面上来理解,很容易让一些初学者先入为主的认为:Se ...

  4. linux 远程 windows 命令:rdesktop vs windows mstsc

    [root@bass tmp]# which rdesktop /usr/bin/rdesktop [root@bass tmp]# rpm -qf /usr/bin/rdesktop rdeskto ...

  5. java 学习之路

    一.基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收 http://www.jcp.org/en/jsr/detail?id=133 http://i ...

  6. platform_device与platform_driver

    转自:http://blog.csdn.net/zhandoushi1982/article/details/5130207 做Linux方面也有三个多月了,对代码中的有些结构一直不是很明白,比如pl ...

  7. ASP.NET MVC 3 使用Model自定义验证的样式

    1.修改jquery.validate.unobtrusive.js 将onError方法修改 //修改的部分 //////////////////////////////////////////// ...

  8. SQL DEVELOPER工具找不到database时的解决

    1.配置SQL DEVELOPER管理工具 这里省略Oracle数据库和PL/SQL Developer的安装步骤,注意在安装PL/SQL Developer软件时,不要安装在Program File ...

  9. poj 2186 有向图强连通分量

    奶牛互相之间有爱慕关系,找到被其它奶牛都喜欢的奶牛的数目 用tarjan缩点,然后判断有向图中出度为0的联通分量的个数,如果为1就输出联通分量中的点的数目,否则输出0. 算法源自kb模板 #inclu ...

  10. 【codevs1993】草地排水 最大流

    [codevs1993]草地排水 题目描述 Description 在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段 ...