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. 案例借鉴 | 某通讯巨头的IT建设方案

    成都联通作为合并重组后的中国联通在成都的分支机构,拥有基础扎实的通信网络和当前最先进技术的WCDMA网络.随着3G和4G业务的发展领先,成都联通凭借其出色的网络能力和服务,在用户中赢得了口碑. 在IT ...

  2. Android开发案例 - 淘宝商品详情

    所有电商APP的商品详情页面几乎都是和淘宝的一模一样(见下图): 采用上下分页的模式 商品基本参数 & 选购参数在上页展示 商品图文详情等其他信息放在下页展示 知识要点 垂直方向的ViewPa ...

  3. [Deprecated!] Android开发案例 - 微博正文

    Deprecated! 更好的实现方式: 使用 android.support.design.widget.CoordinatorLayout. 本文详细介绍如何实现如下图中的微博正文页面效果, 其中 ...

  4. Highchart基础教程-图表配置

    一.图表容器: Highcharts 实例化中绑定容器的两种方式: 1.通过 dom 调用 highcharts() 函数的方式 $("#container").highchart ...

  5. jQuery解决iframe高度自适应代码

    网上查了好多用着都不行,自己搞定了:在包含iframe的页面中加入以下脚本,基本思想是在iframe加载内容后重新设置高度,下面代码尽在IE6中用过,没在其他浏览器中测试. 代码如下: <scr ...

  6. yii2权限控制rbac之rule详细讲解

    作者:白狼 出处:http://www.manks.top/yii2_rbac_rule.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留 ...

  7. SQL Server 2014新特性:分区索引重建

    <single_partition_rebuild_index_option> ::= {     SORT_IN_TEMPDB = { ON | OFF }   | MAXDOP = m ...

  8. Laravel-lumen 配置JWT

    具体步骤参照: [ JWT & Lumen ] 第一步 在项目根目录 执行命令 composer require tymon/jwt-auth第二步 在 bootstrap/app.php 的 ...

  9. Java 数组

    数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. Java语言中提供的数组是用来存储固定大小的同类型元素. 你可以声明一个数组变量,如numbers[100 ...

  10. Microsoft Windows* SDK May 2010 或较新版本(兼容 2010 年 6 月 DirectX SDK)GPU Detect

    原文链接 下载代码样本 特性/描述 日期: 2016 年 5 月 5 日 GPU Detect 是一种简短的示例,演示了检测系统中主要显卡硬件(包括第六代智能英特尔® 酷睿™ 处理器产品家族)的方式. ...