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 - 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.
 
给你两个数N和K,你可以对N进行三种操作,N=N-1,N=N+1,N=N*2,要求在操作数最小的情况下,使N==K,直接BFS暴力搜就行,可以分为以下三种情况进行剪枝(当然,暴力将100000个点搜完也能AC)
令N=N-1时,保证next>=0
令N=N+1时,保证next<=k
令N=N*2时,保证next<=k或者next-e+1<e-no
  当next<=k,一定能减少到达终点时的操作数(当然,N==1时不影响总操作数);
  当next>e,我们这时剩余部分的步数已经确定了(因为只能通过N=N-1使N==K),这时就要比较一下进行该操作和不进行该操作的情况下,e-now就时不进行操作的剩余步数,next-e就是进行操作时的剩余操作数,由于N=N*2也花费了一次操作所以应该加1,而两者相同的情况下自然也不必进行这个操作,毕竟操作数都是一样的
  注意:总共只有100000个点,所以也要保证next<=100000
 
#include <algorithm>
#include <bitset>
//#include <bits/extc++.h>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue> using namespace std;
//using namespace __gnu_pbds #define ll long long
#define maxn 105 int s, e, step[100005];
bool vis[100005]; void bfs()
{
queue<int> q;
q.push(s);
vis[s] = 0;
step[s] = 0;
while (!q.empty())
{
int now = q.front();
q.pop();
int next = now + 1;
if (next <= e && vis[next])
{
vis[next] = false;
step[next] = step[now] + 1;
q.push(next);
}
if (next == e)
{
return;
}
next = now - 1;
if (next >= 0 && vis[next])
{
vis[next] = false;
step[next] = step[now] + 1;
q.push(next);
}
if (next == e)
{
return;
}
next = now << 1;
if ((next <= e || next - e + 1 < e - now) && next <= 100000 && vis[next])
{
vis[next] = false;
step[next] = step[now] + 1;
q.push(next);
}
if (next == e)
{
return;
}
}
} int main()
{
while (~scanf("%d%d", &s, &e))
{
memset(vis, true, sizeof(vis));
if (s >= e)
{
printf("%d\n", s - e); //剪枝
}
else
{
bfs();
printf("%d\n", step[e]);
}
}
return 0;
}

  

Catch That Cow (简单BFS+剪枝)的更多相关文章

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

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

  2. POJ 3278 Catch That Cow(简单BFS)

    题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次 ...

  3. poj3278 Catch That Cow(简单的一维bfs)

    http://poj.org/problem?id=3278                                                                       ...

  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. 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(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

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

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

  9. hdoj 2717 Catch That Cow【bfs】

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

  10. Catch That Cow(BFS)

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

随机推荐

  1. codeforces gym100801 Problem G. Graph

    传送门:https://codeforces.com/gym/100801 题意: 给你一个DAG图,你最多可以进行k次操作,每次操作可以连一条有向边,问你经过连边操作后最小拓扑序的最大值是多少 题解 ...

  2. .NET Core + docker入门

    下载安装docker docker客户端,今天vpn小水管实在是受不了,于是找了国内的下载地址 配置docker加速器 参考博文Docker for windows10 配置阿里云镜像 docker入 ...

  3. python3中map函数

    map函数是Python里面比较重要的函数 map函数后面必须接的是序列(元组/列表/字符串) 在python2中执行一些语句 >>> map(str,[1,2,3,4]) ['1' ...

  4. 北京联通盒子-数码视讯Q7-破解

    准备: 1.数码视讯Q7盒子 2.电焊笔  细电线4跟不同色(可以直接用废旧USB的线) 3.TTL 转 USB线 ,型号: CH340g(自行淘宝购买) 4.安装TTL线的驱动到电脑上(找淘宝商家要 ...

  5. 17.python文件处理

    原文:https://www.cnblogs.com/linhaifeng/articles/5984922.html 文件处理流程: 1. 打开文件,得到文件句柄并赋值给一个变量2. 通过句柄对文件 ...

  6. Vim编辑器Go简单入门

    今天是一次做Go的笔记,一开始直接打开Github上的Go项目然后跑到Wiki位置,然后作者列出了一堆学习Go的资料,这里我 以第一个学习资料https://tour.golang.org/作为Go学 ...

  7. 聊一聊 MySQL 中的事务及其实现原理

    说到数据库,那就一定会聊到事务,事务也是面试中常问的问题,我们先来一个面试场景: 面试官:"事务的四大特性是什么?" 我:"ACID,即原子性(Atomicity).隔离 ...

  8. $loj10156/$洛谷$2016$ 战略游戏 树形$DP$

    洛谷loj Desription Bob 喜欢玩电脑游戏,特别是战略游戏.但是他经常无法找到快速玩过游戏的方法.现在他有个问题. 现在他有座古城堡,古城堡的路形成一棵树.他要在这棵树的节点上放置最少数 ...

  9. Python数据分析:手把手教你用Pandas生成可视化图表

    大家都知道,Matplotlib 是众多 Python 可视化包的鼻祖,也是Python最常用的标准可视化库,其功能非常强大,同时也非常复杂,想要搞明白并非易事.但自从Python进入3.0时代以后, ...

  10. Okhttp解析—Okhttp概览

    Okhttp解析-Okhttp概览 Okhttp作为目前Android使用最为广泛的网络框架之一,我们有必要去深入了解一下,本文是Okhttp解析的第一篇,主要是从宏观上认识Okhttp整个架构是如何 ...