Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 80273   Accepted: 25290

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 - 1 or + 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.

思路

题意:给定起点和终点,有三种走的方式,假设当前为 now,那么可以选择下一次到达 now - 1 或 now + 1  或 2*now,问起点到终点最少需要几步。

题解:bfs,下一个状态即为三种可选择的位置。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 100005;
int step[maxn],que[maxn],vis[maxn];

int bfs(int st,int ed)
{
	int E = 0,F = 0;
	que[F++] = st;
	vis[st] = 1;
	for (;;)
	{
		int now = que[E];
		if (now == ed)	return step[now];
		if (now + 1 < maxn && !vis[now + 1])	step[now + 1] = step[now] + 1,vis[now + 1] = 1,que[F++] = now + 1;
		if (now - 1 >= 0 &&!vis[now - 1])	step[now - 1] = step[now] + 1,vis[now - 1] = 1,que[F++] = now - 1;
		if (2*now < maxn && !vis[2*now])	step[2*now] = step[now] + 1,vis[2*now] = 1,que[F++] = 2 * now;
		E++;
	}
}

int main()
{
	int N,K;
	memset(vis,0,sizeof(vis));
	scanf("%d%d",&N,&K);
	if (N >= K)	printf("%d\n", N - K);
	else	printf("%d\n",bfs(N,K));
	return 0;
}

  

POJ 3278 Catch That Cow(bfs)的更多相关文章

  1. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...

  2. POJ 3278 Catch That Cow(BFS 剪枝)

    题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出 ...

  3. POJ——3278 Catch That Cow(BFS队列)

    相比于POJ2251的三维BFS,这道题做法思路完全相同且过程更加简单,也不需要用结构体,check只要判断vis和左右边界的越界情况就OK. 记得清空队列,其他没什么好说的. #include< ...

  4. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

  5. poj 3278(hdu 2717) Catch That Cow(bfs)

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

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

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

  7. POJ 3278 Catch That Cow(求助大佬)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 109702   Accepted: 34255 ...

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

随机推荐

  1. 关getClass().getClassLoader()

    InputStream   is   =   getClass().getClassLoader().getResourceAsStream("helloworld.properties&q ...

  2. 错误:当你使用id作为sharepoint的自定义页面的查询参数时,总会提示项目不存在!

    No item exists at http://SERVER/SITE/mypage.aspx?ID=1. It may have been deleted or renamed by anothe ...

  3. iOS获取网络图片大小

    在iOS开发过程中经常需要通过网络请求加载图片,有时,需要在创建UIImageView或UIButton来显示图片之前需要提前知道图片的尺寸,根据图片尺寸创建对应大小的控件.但是对于网络图片来说,要想 ...

  4. 关于jni编译32位、64位动态库(Android.mk和Application.mk文件)

    最近新项目需要编译64位的动态库,这里记录如何配置. 在jni目录下加入Android.mk和Application.mk文件. Application.mk APP_ABI := armeabi a ...

  5. iOS提交AppStore后申请加急审核

    提交审核后进去下面链接申请加急审核链接:https://developer.apple.com/appstore/contact/appreviewteam/index.html 在I would l ...

  6. Android 开源库和项目 2

    1.带尾巴的RecyclerViewPager 特点:1.像viewPager一样滑动一次就滑动一页 2.像画廊gallery一样,滑动一次可以滑动很多页 3.竖向滑动 4.支持点击事件,没有错乱   ...

  7. ASP.NET MVC 3 技术(九) 301永久重定向不带www域名到带www的域名

    在 .net 4 中实现永久重定向非常容易,可以参考ASP.NET MVC3 技术(四) 永久重定向方法.今天主要说明下怎么在 asp.net mvc 3 实现从带www的域名永久重定向到不带www的 ...

  8. 1.2 - C#语言习惯 - 用运行时常量readonly而不是编译期常量const

    C#中有两种类型的常量:编译期常量和运行时常量.二者有着截然不同的行为,使用不当将会带来性能上或正确性上的问题. 这两个问题最好都不要发生,不过若难以同时避免的话,那么一个略微慢一些但能保证正确的程序 ...

  9. web工程常用路径的获取方法

    此文章是基于 搭建SpringMVC+Spring+Hibernate平台 一. 利用 Spring 取得web工程根路径 1. web.xml 中添加如下: <context-param> ...

  10. CentOS7 安装Nginx

    由于需要,这段时间学一点“nginx”.关于nginx就不介绍了,http://wiki.nginx.org/Main有非常详细的介绍.安装等. 安装软件我习惯到官网下载源码,http://nginx ...