hdu 1548 A strange lift 宽搜bfs+优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548
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"?
#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+优先队列的更多相关文章
- HDU 1548 A strange lift(最短路&&bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- HDU 1548 A strange lift(Dijkstra,简单BFS)
题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数, ...
- 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 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...
- hdu 1548 A strange lift (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 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 ...
- HDU 1548 A strange lift (广搜)
题目链接 Problem Description There is a strange lift.The lift can stop can at every floor as you want, a ...
- 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 搜索
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
随机推荐
- dataGridView 如何默认选中第一行
datagridview默认选中第一行方法: this.dataGridView1.Rows[0].Selected = true; datagridview 去除 默认选中第一行方法:在绑定data ...
- 【转】详解JavaScript中的this
ref:http://blog.jobbole.com/39305/ 来源:foocoder 详解JavaScript中的this JavaScript中的this总是让人迷惑,应该是js众所周知的坑 ...
- SQL SERVER中查询参数为空(null)时默认查询所有的实现
最近在项目中碰到一个比较有意思的问题,网上查找了一些方法,在这里总结分享一下. 我们经常会碰到这样的场景:需要查询数据,有一些查询条件,但是查询的时候,我们希望在某个条件为空的时候,则不筛选这个条件, ...
- 一个ListView中显示不同的item(分组)
MainActivity: package com.zzw.qqgroup; import java.util.ArrayList; import java.util.HashMap; import ...
- 关于FragmentManager findFragmentById 返回nul
先看Fragment的两种生成方式 一.用xml标签生成 在fragment的宿主activity中添加xml标签 <fragment android:id="@+id/fragmen ...
- spring debug
DispatcherServlet{ getHandler()}handlerMappings{ RequestMappingHandlerMapping BeanNameUrlHandlerMapp ...
- 《大话设计模式》ruby版代码:策略模式
需求: 商场收银软件,根据客户购买物品的单价和数量,计算费用,会有促销活动,打八折,满三百减一百之类的. 一,使用工厂模式. # -*- encoding: utf-8 -*- #现金收费抽象类 cl ...
- oracle 各种问题排查
一.ORA-00257 ORA-00257归档日志写满,最简单方法.可以更改归档的大小. 二.job不自动运行解决方法 http://www.cnblogs.com/xbding/p/5861443. ...
- 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 ...
- Ubuntu14.04 Objective-C hello world
1. Install GNUstep sudo apt-get install gnustep gnustep-devel 2. Write hello world program, and save ...