hdu A strange lift
有起点和终点,有方向,有最少次数,所以这道题很明显是一道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的更多相关文章
- 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 There is a strange lift.The lift can stop can at ...
- 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 (最短路/Dijkstra)
题目链接: 传送门 A strange lift Time Limit: 1000MS Memory Limit: 32768 K Description There is a strange ...
- 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 搜索
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu 1548 A strange lift (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 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 ...
- 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 ...
随机推荐
- 在Python脚本中判断Python的版本
引自:http://segmentfault.com/q/1010000000127878 如果是给人读,用 sys.version,如果是给机器比较,用 sys.version_info,如果是判断 ...
- iOS constraint被应用于view上的时间
在viewdidload时,constraint是没有被应用的,之后在layoutSubviews时,系统应用了constraint.但是我感觉在viewWillLayoutSubviews函数时就已 ...
- java 和 objective-c 动态获得类型信息
详细信息,请参看android官方文档中的class类的介绍 ,和苹果的官方文档Objective-C Runtime Reference java中常常听到反射reflection,在java的cl ...
- Delphi经验总结(1)
先人的DELPHI基础开发技巧 ◇[DELPHI]网络邻居复制文件 uses shellapi; copyfile(pchar('newfile.txt'),pchar('//computername ...
- codeforces B. Simple Molecules 解题报告
题目链接:http://codeforces.com/problemset/problem/344/B 题目意思:这句话是解题的关键: The number of bonds of an atom i ...
- Ubuntu 用户安装 MATE
MATE 是经典桌面 Gnome 2 的分支,该桌面按照 Windows 用户操作习惯设计,适合于 Windows 转投 Linux 的初级用户,MATE 做了功能改进和新增功能.如:增加窗口管理 ...
- kindeditor的简单使用
上传到云: 一.引入kindeditor <%@ page language="java" contentType="text/html; charset=UTF- ...
- 【python】pathlib库
pathlib在python3.2以上开始默认支持,在python2.7中如果要使用需要安装 pip install pathlib pathlib更多参考资料:http://pathlib.read ...
- UVA 11827 Maximum GCD (输入流)
题目:传送门 题意:求n个数的最大公约数,暴力不会超时,难点在没有个数控制的输入. 题解:用特殊方法输入. #include <iostream> #include <cmath&g ...
- AJAX,JSON搜索智能提示
效果 开发结构参考AJAX,JSON用户校验 主要有两个核心文件 1,处理输入字符,进行后台搜索的servlet Suggest.java package org.guangsoft.servlet; ...