Catch That Cow

POJ - 3278

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.

依旧是广搜的模板题

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
/*priority_queue<int,vector<int> ,greater<int> >q;*/
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
bool vis[maxn];
int step[maxn];
int bfs(int s,int e)
{queue<int> q;q.push(s);step[s]=0;vis[s]=true;while(!q.empty()){int front=q.front();q.pop();if(front+1>0&&front+1<=1e5&&vis[front+1]==false){q.push(front+1);vis[front+1]=true;step[front+1]=step[front]+1;if(front+1==e)return step[front+1];}if(front-1>0&&front-1<=1e5&&vis[front-1]==false){q.push(front-1);vis[front-1]=true;step[front-1]=step[front]+1;if(front-1==e)return step[front-1];}if(front*2>0&&front*2<=1e5&&vis[front*2]==false){q.push(front*2);vis[front*2]=true;step[front*2]=step[front]+1;if(front*2==e)return step[front*2];}}
}
int main()
{//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int N,K;cin>>N>>K;ms(vis);ms(step);if(N>=K)cout<<N-K<<endl;elsecout<<bfs(N,K)<<endl;return 0;
}

Catch That Cow【广搜】相关推荐

  1. Catch That Cow——广搜

    Catch That Cow Problem Description Farmer John has been informed of the location of a fugitive cow a ...

  2. poj 3278 Catch That Cow 广搜

    hdu 2717 Catch That Cow,题目链接 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...

  3. POJ 3728 Catch That Cow (广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 42564   Accepted: 13225 ...

  4. POJ 3278 / hdu 2717 Catch That Cow (广搜)

    POJ 3278 HDU 2717 广搜题,用一个数组标记就可以过,不标记的话会超内存. 另外,poj的数据要比hdu强一些,比如0 100,这种数据.不特判的话会RE.不过如果不特判,在poj上用C ...

  5. poj 3278 Catch That Cow(广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45087   Accepted: 14116 ...

  6. POJ - 3278 Catch That Cow 简单搜索

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

  7. Catch That Cow抓住那只奶牛(BFS,广度优先搜索)

    # **Catch That Cow(抓住那只奶牛)** [题意]:农场主要去追一直逃跑的牛,每次移动有三种选择,位置加一,位置减一,位置乘二,要找到最快的方法让农夫追到牛(也就是移动的次数最少,这个 ...

  8. 深搜广搜专题【DFS】【BFS】

    深搜广搜专题 又是一年专题时,这次的专题是BFS和DFS,我们刚加入acm时的噩梦,然而现在已经写起来很舒服了(OS:那你还A不出题?) BFS和DFS都是通过对所有的点进行遍历来得到结果的,是一种比 ...

  9. hdu 2717 Catch That Cow

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

最新文章

  1. Linux 防火墙的基本认识
  2. Dynamics 365 on-premises 安装
  3. python 系统学习笔记(十二)---os os.path os.walk
  4. 数据之光 · 安全未来 | 第四届中国数据安全治理高峰论坛圆满召开!
  5. Bootstrap 编译版文件结构
  6. CWinApp的应用与理解
  7. Java高级工程师学习要求
  8. 图片的格式怎么改?教你快速转格式的方法?
  9. 最强推荐:阿里P7级别面试经验总结,进阶学习资料!
  10. 导数、微分、偏导数、全微分、方向导数、梯度的定义与关系
  11. \u5168\u56fd\u7f8e\u5bb9\u5927\u592b数据采集数据(\u82b1\u5bb9\u7f51 huaroo 公开数据),爬虫120例之26例
  12. 谷歌大牛Jeff Dean亲自撰文:深度学习研究的黄金十年
  13. 如何让域用户安装需要管理员权限的软件
  14. 边读边捋【july的】海量数据处理面试题
  15. 如何冻结excel指定行和列
  16. Windows网络诊断和配置常用命令详解
  17. codeforces 69A(Young Physicist) Java
  18. Knowledge Graph Convolutional Networks for Recommender Systems with Label Smoothness Regularization
  19. 建筑行业必看,一招学会工地管理诀窍
  20. 数据迁移怎么测,都有哪些步骤?

热门文章

  1. 求qt如何解决小数计算过程中的精度丢失
  2. PWN题[强网先锋]no_output
  3. CTF-Bugku逆向题Android方法归纳
  4. 挂机宝和网站服务器,挂机宝SSD硬盘vs普通硬盘访问官网哪个快?
  5. pbl和sbl_谈PBL和SBL教学法结合模式
  6. labuladong 的算法小抄_来自GitHub 68.8k star的硬核算法教程
  7. 计算机室内设计cad实践报告,cad实习报告3000字
  8. java for foreach 效率_Java中LinkedList的fori和foreach效率比较
  9. c语言手游常用代码,c语言源代码【操作流程】
  10. 经济学原理 下载 曼昆_2021南开经济学考研全年规划