题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548

There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist.
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"?
 
题意描述:n层楼(1<=n<=200),每层楼一个数字Ki和两个按钮UP和DOWN,如果你在A层楼,A层楼的数字Ka=3,你可以按下UP按钮到A+Ka层楼,按下DOWN按钮到A-Ka层楼(前提是A+Ka和A-Ka都在n的范围里)。 现在你在A层楼,要到目的地B层楼,问按下按钮的最少次数。
算法分析:直接bfs暴搜即可,由于涉及到最少次数问题,我习惯性的用到了优先队列处理。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int M = +; int n,A,B;
int an[maxn];
struct node
{
int cnt,id;
int value;
friend bool operator < (node a,node b)
{
return a.cnt > b.cnt;
}
}cur,tail; int vis[maxn];
int bfs()
{
priority_queue<node> Q;
cur.id=A ;cur.cnt= ;
cur.value=an[A];
Q.push(cur);
memset(vis,,sizeof(vis));
vis[A]=;
int count=;
while (!Q.empty())
{
cur=Q.top() ;Q.pop() ;
if (cur.id==B) return cur.cnt; //cout<<cur.id<<" "<<an[cur.id]<<" "<<cur.cnt<<endl;
tail.id=cur.id+an[cur.id];
if (tail.id>= && tail.id<=n && !vis[tail.id])
{
tail.value=an[tail.id];
tail.cnt=cur.cnt+;
vis[tail.id]=;
Q.push(tail);
} tail.id=cur.id-an[cur.id];
if (tail.id>= && tail.id<=n && !vis[tail.id])
{
tail.value=an[tail.id];
tail.cnt=cur.cnt+;
vis[tail.id]=;
Q.push(tail);
}
}
return -;
} int main()
{
while (scanf("%d",&n)!=EOF && n)
{
scanf("%d%d",&A,&B);
for (int i= ;i<=n ;i++) scanf("%d",&an[i]);
printf("%d\n",bfs());
}
return ;
}

hdu 1548 A strange lift 宽搜bfs+优先队列的更多相关文章

  1. HDU 1548 A strange lift(最短路&&bfs)

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

  2. HDU 1548 A strange lift(Dijkstra,简单BFS)

    题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数, ...

  3. hdu 1548 A strange lift

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

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

  5. hdu 1548 A strange lift (bfs)

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

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

  7. HDU 1548 A strange lift (广搜)

    题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...

  8. HDU 1548 A strange lift (Dijkstra)

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

  9. HDU 1548 A strange lift 搜索

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

随机推荐

  1. Cassandra 的压缩策略STCS,LCS 和 DTCS

    更新说明: 本文编写时最新的Cassandra版本为2.2,最新的稳定版本为2.1.8 2016年6月23日,增加一篇译文,当下最新版本为3.7 最新的Cassandra 2.1 或者更高的版本支持3 ...

  2. 配置pxe 自动化安装centos6.7

    dhcp服务器是pxe自动化安装的必要条件,因此先搞定dhcp服务器,yum -y install dhcp,  rpm -ql dhcp查看安装了哪些包,less /etc/dhcp/dhcpd.c ...

  3. DevExpress之ASPxGridView笔记(1)

    1.设置Row(某列)输出格式,例如,在数字前加美元符.每3位以逗号隔开及设置小数点后两位: <dx:GridViewDataTextColumn FieldName="SHOUHUI ...

  4. centos下安装php环境

    centos下安装php环境 安装apache yum install httpd-devel 启动apache /etc/init.d/httpd start 安装mysql yum install ...

  5. Linux Shell脚本编程的注意事项

    Linux下(Shell脚本 http://www.jbxue.com/jb/shell/)编程的一些注意事项,如编程风格.命名风格等. 一.常用技巧 ssh user@server bash < ...

  6. js JSON对象属性

    json对象是是一种用于原生json分析的对象,无法对其进行构造函数调用,用java术语 来说,它相当于能够直接使用类方法的工具类JSON对象的属性parse(text[,reviver]);对参数t ...

  7. C++多态性的浅析

    多态性是C++的一个重要特性,[不扯淡直接进入正题] 灵活运用多态,首先得知道类之间的继承.  当B继承了A类后,一般都是公有继承.  B的实例化对象的内存空间结构若是了解 就可以合理利用多态了. A ...

  8. CSS 设置TABLE 表格 边框

    /*table列表 合并边框设置*/ .tablelist { border-collapse:collapse; } /*table列表 设置边框宽度及颜色*/ .tablelist td { bo ...

  9. jquery 点击页面其他地方实现隐藏菜单功能

    1.给页面文档添加一个点击事件函数,在函数内实现隐藏菜单功能. $('html').click(function(){//Hide the menus if visible});//用$(docume ...

  10. word中让首页和目录不显示页码的方法

    在正文前一页,插入->分隔符->下一页,然后插入页码,取消与前一页页眉的链接,删除首页和目录的页码即可