题目链接: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. foreach 和 list.foreach 初步测试

    单纯从速度上讲 小数据量下foreach 较快,list.Foreach 由于 public void ForEach(Action<T> action) { ; i <this._ ...

  2. Centos 7.1+CDH5.7.2全部署流程

    前期准备: JDK环境 版本:jdk-8u101-linux-x64.rpm 下载地址:oracle官网 mysql rpm包:http://dev.mysql.com/get/Downloads/M ...

  3. php数组去重复代码

    php数组去重复数据示例,有时候获得的php数组中总是出现value重复的,使用下面的方法就可以去掉重复数据 以数字开头的重复数据如: Array (  [0] => 100  [k1] =&g ...

  4. js闭包理解实例小结

    Js闭包 闭包前要了解的知识  1. 函数作用域 (1).Js语言特殊之处在于函数内部可以直接读取全局变量 <script type="text/javascript"> ...

  5. 一个php函数,能够遍历一个文件夹下的所有文件和子文件夹

    <?phpfunction my_scandir($dir){    $files=array();    if(is_dir($dir))     {        if($handle=op ...

  6. PHP上传图片如何防止图片木马?

    segmentfault回答: http://segmentfault.com/q/1010000000507750 一. 其实识别图片木马是很困难的,可以在一张正常的图片里加入一句话木马. 但是只要 ...

  7. 通过JAVA代码获取手机的一些基本信息(本机号码,SDK版本,系统版本,手机型号)

    代码如下: package com.zzw.getPhoneInfos; import android.app.Activity; import android.content.Context; im ...

  8. android 特效UI实现

    弧形菜单 https://github.com/daCapricorn/ArcMenu

  9. C# 验证IP是否正确简易方法 源代码

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. SQL 跟踪方法相关介绍

    oracle sql跟踪方法:1.sql_trace打开跟踪:alter session set sql_trace=true;为跟踪文件做标记:alter session set tracefile ...