题目链接: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. dataGridView 如何默认选中第一行

    datagridview默认选中第一行方法: this.dataGridView1.Rows[0].Selected = true; datagridview 去除 默认选中第一行方法:在绑定data ...

  2. 【转】详解JavaScript中的this

    ref:http://blog.jobbole.com/39305/ 来源:foocoder 详解JavaScript中的this JavaScript中的this总是让人迷惑,应该是js众所周知的坑 ...

  3. SQL SERVER中查询参数为空(null)时默认查询所有的实现

    最近在项目中碰到一个比较有意思的问题,网上查找了一些方法,在这里总结分享一下. 我们经常会碰到这样的场景:需要查询数据,有一些查询条件,但是查询的时候,我们希望在某个条件为空的时候,则不筛选这个条件, ...

  4. 一个ListView中显示不同的item(分组)

    MainActivity: package com.zzw.qqgroup; import java.util.ArrayList; import java.util.HashMap; import ...

  5. 关于FragmentManager findFragmentById 返回nul

    先看Fragment的两种生成方式 一.用xml标签生成 在fragment的宿主activity中添加xml标签 <fragment android:id="@+id/fragmen ...

  6. spring debug

    DispatcherServlet{ getHandler()}handlerMappings{ RequestMappingHandlerMapping BeanNameUrlHandlerMapp ...

  7. 《大话设计模式》ruby版代码:策略模式

    需求: 商场收银软件,根据客户购买物品的单价和数量,计算费用,会有促销活动,打八折,满三百减一百之类的. 一,使用工厂模式. # -*- encoding: utf-8 -*- #现金收费抽象类 cl ...

  8. oracle 各种问题排查

    一.ORA-00257 ORA-00257归档日志写满,最简单方法.可以更改归档的大小. 二.job不自动运行解决方法 http://www.cnblogs.com/xbding/p/5861443. ...

  9. poj 1564 Sum It Up

    题目连接 http://poj.org/problem?id=1564 Sum It Up Description Given a specified total t and a list of n ...

  10. Ubuntu14.04 Objective-C hello world

    1. Install GNUstep sudo apt-get install gnustep gnustep-devel 2. Write hello world program, and save ...