HUD 2717 Catch That Cow
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12798 Accepted Submission(s): 3950
Problem Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
- Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
- Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
Input
Line 1: Two space-separated integers: N and K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4
Hint
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
Source
USACO 2007 Open Silver
解析:简单BFS。易知农夫走的范围不会超过2*k,超过2*k就得不到最小值。
```
#include
#include
const int MAXN = 100000+5;
int dis[2MAXN];
int q[2MAXN];
int bfs(int n, int k)
{
memset(dis, -1, sizeof dis);
dis[n] = 0;
int l = 0, r = 0;
q[r++] = n;
while(l < r){
int h = q[l++];
if(h == k)
return dis[k];
int tmp = h-1;
if(tmp >= 0 && dis[tmp] == -1){
q[r++] = tmp;
dis[tmp] = dis[h]+1;
}
tmp = h+1;
if(tmp <= 2k && dis[tmp] == -1){
q[r++] = tmp;
dis[tmp] = dis[h]+1;
}
tmp = h2;
if(tmp <= 2*k && dis[tmp] == -1){
q[r++] = tmp;
dis[tmp] = dis[h]+1;
}
}
}
int main()
{
int n, k;
while(~scanf("%d%d", &n, &k)){
int res = bfs(n, k);
printf("%d\n", res);
}
return 0;
}
HUD 2717 Catch That Cow的更多相关文章
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- 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,最先 ...
- HDU 2717 Catch That Cow(常规bfs)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- hdoj 2717 Catch That Cow【bfs】
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2717 Catch That Cow(广搜bfs)
题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...
- 杭电 HDU 2717 Catch That Cow
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Hdoj 2717.Catch That Cow 题解
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
随机推荐
- ARM菜鸟:JLINK与JTAG的区别
调试ARM,要遵循ARM的调试接口协议,JTAG就是其中的一种.当仿真时,IAR.KEIL.ADS等都有一个公共的调试接口,RDI就是其中的一种,那么我们如何完成RDI-->ARM调试协议(JT ...
- 存储入门 – RAID技术(大图解释)
对于RAID,一直都知道个概念,但是对于细节没有去仔细的研究过.正好昨天Training的时候, 老师讲解了RAID的内容,所以顺便就整理一下.很多内容都是参考了ISMv2这本书. RAID中用到的技 ...
- 编译FreePascal和Lazarus
一,准备目录假设我们准备将整个FreePascal环境安装到D盘的fpc目录下,那么我们需要创建以下几个目录:d:\fpc_svn\bind:\laz_svn二,准备环境1,安装SVN客户端Torto ...
- Java:字符串类String的功能介绍
在java中,字符串是一个比较常用的类,因为代码中基本上处理的很多数据都是字符串类型的,因此,掌握字符串类的具体用法显得很重要了. 它的主要功能有如下几种:获取.判断.转换.替换.切割.字串的获取.大 ...
- PCA基础理解
- 标准类型内建函数 type()介绍
我们现在来正式介绍一下 type().在Python2.2 以前, type() 是内建函数.不过从那时起,它变成了一个“工厂函数”.在本章的后面部分我们会讨论工厂函数, 现在你仍然可以将type() ...
- SGU 385 Highlander(期望)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=385 题意: 思路: double f[N][N][N],g[N][N],A[N][ ...
- 《OD学hadoop》Hadoop前置
一.Hadoop 前置课程 1. Linux系统,基本命令 2. Java语言,JavaSE相关知识 3. MySQL基本的DML和DDL SQL on Hadoop
- D3D中深度测试和Alpha混合的关系
我在学习D3D的深度测试和Alpha混合的时候,有一些遗憾.书上提供的例子里说一定要先渲染不透明物体,再渲染透明物体,对渲染状态的设置也有特殊要求.我看的很晕.自己查图形学的书,上网找资料,结果还是糊 ...
- Android中GridView拖拽的效果【android进化三十六】
最 近看到联想,摩托罗拉等,手机launcher中有个效果,进入mainmenu后,里面的应用程序的图标可以拖来拖去,所以我也参照网上给的代码,写了 一个例子.还是很有趣的,实现的流畅度没有人家的 ...