HDU 1548 (最基础的BFS了) A strange lift
这是一维的BFS,而且没有什么变形,应该是最基础的BFS了吧
题意:
有这样一个奇葩的电梯,你在第i层的时候你只能选择上或者下Ki层,也就是你只能从第i层到达i+Ki或者i-Ki层。当然电梯最低只能在1层最高只能在n层。
给出起点和终点问最少需要多少次才能到达终点,如果不能到达输出-1
没有什么好解释的了,如此单纯的一道题,只要不是太粗心就能A过去
//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = + ;
struct Point
{
int h;
int times;
}qu[maxn];
int n, from, to, go[maxn], vis[maxn];
int head, tail;
int dir[] = {, -}; void BFS(void)
{
head = , tail = ;
qu[].h = from, qu[].times = ;
while(head < tail)
{
if(qu[head].h == to)
{
printf("%d\n", qu[head].times);
return;
}
for(int i = ; i < ; ++i)
{
int hh = qu[head].h + go[qu[head].h]*dir[i];
if(hh> && hh<=n && (!vis[hh]))
{
vis[hh] = ;
qu[tail].h = hh;
qu[tail++].times = qu[head].times + ;
}
}
++head;
}
printf("-1\n");
} int main(void)
{
#ifdef LOCAL
freopen("1548in.txt", "r", stdin);
#endif while(scanf("%d", &n) == && n)
{
scanf("%d%d", &from, &to);
for(int i = ; i <= n; ++i)
scanf("%d", &go[i]);
memset(vis, , sizeof(vis));
BFS();
}
return ;
}
代码君
HDU 1548 (最基础的BFS了) A strange lift的更多相关文章
- 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 楼梯 bfs或最短路 dijkstra
http://acm.hdu.edu.cn/showproblem.php?pid=1548 Online Judge Online Exercise Online Teaching Online C ...
- 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(最短路&&bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
随机推荐
- php获取网页header信息的4种方法
php获取网页header信息的方法多种多样,就php语言来说,我知道的方法有4种, 下面逐一献上. 方法一:使用get_headers()函数 推荐指数: ★★★★★ get_header方法最简单 ...
- (转)Android: NDK编程入门笔记
转自: http://www.cnblogs.com/hibraincol/archive/2011/05/30/2063847.html 为何要用到NDK? 概括来说主要分为以下几种情况: 1. 代 ...
- sessionID的本质
SessionID的本质 一.客户端用cookie保存了sessionID 客户端用cookie保存了sessionID,当我们请求服务器的时候,会把这个sessionID一起发给服务器,服务器会到内 ...
- 【hdu3579-Hello Kiki】拓展欧几里得-同余方程组
http://acm.hdu.edu.cn/showproblem.php?pid=3579 题解:同余方程组的裸题.注意输出是最小的正整数,不包括0. #include<cstdio> ...
- MongoDB (四) MongoDB 数据模型
在 MongoDB 中的数据有灵活的模式.在相同集合中文档并不需要有相同的一组字段或结构的公共字段的集合,文档可容纳不同类型的数据. MongoDB设计模式的一些考虑 可根据用户要求设计架构. 合并对 ...
- volatile小记
1.要使volatile变量提供理想的线程安全,必须同时满足以下两个条件: 1).对变量的写操作不依赖于当前值: 2).该变量没有包含在具有其他变量的不变式中. 第一个条件的限制使volatile变量 ...
- 【Apache运维基础(3)】虚拟主机配置说明
建议在主配置文件中增加一句 Include conf/vhosts/*.conf 然后就在vhosts目录下添加虚拟主机配置文件 在配置前打开NameVirtualHost *:80注释,注意此处要与 ...
- 【web性能】页面呈现、重绘、回流
在讨论页面重绘.回流之前.需要对页面的呈现流程有些了解,页面是怎么把html结合css等显示到浏览器上的,下面的流程图显示了浏览器对页面的呈现的处理流程.可能不同的浏览器略微会有些不同.但基本上都是类 ...
- 自己动手实现java中cache
实现思路: 创建一个静态Hashtable用于保存key和value,对于cache过期后的方法回调,在cache过期后,再访问cache的时候进行,避免了使用定时器轮询过期时间,进行cache清除的 ...
- Netty4.x中文教程系列(五)编解码器Codec
Netty4.x中文教程系列(五)编解码器Codec 上一篇文章详细解释了ChannelHandler的相关构架设计,版本和设计逻辑变更等等. 这篇文章主要在于讲述Handler里面的Codec,也就 ...