Catch That Cow

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7147    Accepted Submission(s): 2254
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.
 

#include <stdio.h>
#include <queue>
#include <string.h>
#define maxn 100002
using std::queue; struct Node{
int pos, step;
};
bool vis[maxn]; void move(Node& tmp, int i)
{
if(i == 0) --tmp.pos;
else if(i == 1) ++tmp.pos;
else tmp.pos <<= 1;
} bool check(int pos)
{
return pos >= 0 && pos < maxn && !vis[pos];
} int BFS(int n, int m)
{
if(n == m) return 0;
memset(vis, 0, sizeof(vis));
queue<Node> Q;
Node now, tmp;
now.pos = n; now.step = 0;
Q.push(now);
vis[n] = 1;
while(!Q.empty()){
now = Q.front(); Q.pop();
for(int i = 0; i < 3; ++i){
tmp = now;
move(tmp, i);
if(check(tmp.pos)){
++tmp.step;
if(tmp.pos == m) return tmp.step;
vis[tmp.pos] = 1;
Q.push(tmp);
}
}
}
} int main()
{
int n, m;
while(scanf("%d%d", &n, &m) == 2){
printf("%d\n", BFS(n, m));
}
return 0;
}

HDU2717 Catch That Cow 【广搜】的更多相关文章

  1. hdu 2717 Catch That Cow(广搜bfs)

    题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...

  2. hdu2717 Catch That Cow

    http://acm.hdu.edu.cn/showproblem.php?pid=2717 //水搜... #include<stdio.h> #include<math.h> ...

  3. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  4. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. Catch That Cow(BFS广搜)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  6. Catch That Cow(广搜)

    个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John h ...

  7. poj 3278 Catch That Cow (广搜,简单)

    题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...

  8. 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 ...

  9. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

随机推荐

  1. java 解压zip java.lang.IllegalArgumentException: MALFORMED 错误

    ava.lang.IllegalArgumentException: MALFORMED at java.util.zip.ZipCoder.toString(Unknown Source) at j ...

  2. redis实现秒杀demo

    代码 package com.prosay.redis; import java.util.List; import redis.clients.jedis.Jedis; import redis.c ...

  3. 行尸走肉第八季/全集The Walking Dead迅雷下载

    <行尸走肉第八季>将在10月22号回归播出, 将于5月1日起正式开始拍摄!目前剧组已开始招募客串演员和丧尸群演,第八季拍摄时长初步预计会持续到11月16日.而这三位已确认为<行尸走肉 ...

  4. 女子监狱第一季/全集Orange Is the New Black迅雷下载

    本季第一季 Orange Is the New Black 1 (2013) 看点:该剧描述主人公Piper Chapman(Taylor Schilling)在大学里结识了毒贩Alex(Laura ...

  5. Java反编译工具Jad详解

    做项目过程中需要反编译一个jar包,于是作了一些学习,记录下来. Jad(JAva Decompiler)是一个Java的反编译器,可以通过命令行把Java的class文件反编译成源代码. 如果你在使 ...

  6. 【已解决】Android微信开放平台,申请移动应用的 应用签名 如何获取

    在微信开放平台,申请移动应用的时候: https://open.weixin.qq.com/cgi-bin/appcreate?t=manage/createMobile&type=app&a ...

  7. REST构架风格介绍之二:CRUD

    转载自:Todd  Wei    http://www.cnblogs.com/weidagang2046/archive/2009/05/09/1453065.html 上一节我们通过两个例子初步体 ...

  8. emouse思·睿—评论与观点整理之二

    虽说我主要做的硬件,平时的兴趣爱好比较关注移动互联网,混迹于虎嗅.爱范儿.雷锋网.36Kr.cnBeta.瘾科技.i黑马.TechWeb等这类科技以及创业媒体,遗憾的是系统的去写的并不多,好在还算充分 ...

  9. JavaScript:Number 对象

    ylbtech-JavaScript:Number 对象 1. Number 对象返回顶部 Number 对象 Number 对象是原始数值的包装对象. 创建 Number 对象的语法: var my ...

  10. 前端基于jquery的UI框架

    正在做的一个项目选择jquery作为前端js核心库.然后就想选一个基于jquery的ui库,然后悲催的事情发生了. 至于为什么使用jquery,一是因为不想为授权费用,而又不想引起可能法律纠纷:另一方 ...