Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 97240   Accepted: 30519

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.
思路:bfs入门题,用到一些剪枝,之前没怎么用bfs不是很熟练,看了些其他大佬的代码,每个点都有三种情况,将三种情况入队,然后标记这个点已经访问,最后最先达到k点的必为最短。直接输出就行了
实现代码:
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<list>
using namespace std;
#define me0(x) memset(x,0,sizeof(x))
#define pb(x) push_back(x)
#define ll long long
const int Mod = 1e9+7;
const int inf = 1e9;
const int Max = 2e5+10;
vector<int>vt[Max];
queue<int>q;
int dx[] = {-1, 1,  0, 0};
int dy[] = { 0, 0, -1, 1};
//void exgcd(ll a,ll b,ll& d,ll& x,ll& y){if(!b){d=a;x=1;y=0;}else{exgcd(b,a%b,d,y,x);y-=x*(a/b);}}
//ll inv(ll a,ll n){ll d, x, y;exgcd(a,n,d,x,y);return (x+n)%n;}  ��Ԫ
//int gcd(int a,int b)  {  return (b>0)?gcd(b,a%b):a;  }  ��С��Լ
//int lcm(int a, int b)  {  return a*b/gcd(a, b);   }    ������
int cnt[Max];
bool vis[Max];void bfs(int l,int r)
{q.push(l);vis[l] = 1;cnt[l] = 0;while(!q.empty()){int x = q.front();q.pop();if(x==r){cout<<cnt[r]<<endl;break;}if(x-1>=0&&!vis[x-1]){vis[x-1] = 1;q.push(x-1);cnt[x-1] = cnt[x] + 1;}if(x<=r&&!vis[x+1]){vis[x+1] = 1;q.push(x+1);cnt[x+1] = cnt[x] + 1;}if(x<=r&&!vis[x*2]){vis[x*2] = 1;q.push(x*2);cnt[x*2] = cnt[x] + 1;}}
}int main()
{int n,m;cin>>n>>m;me0(vis);bfs(n,m);return 0;
}

转载于:https://www.cnblogs.com/kls123/p/7411050.html

poj3278 【BFS】相关推荐

  1. 【BFS】魔板(c++基础算法)

    本专栏上一篇:[BFS]八数码问题(c++基础算法) 目录 一.读题 ①题面 ②题意 三.做题 ①算法原理 ②算法实现 Ⅰ三种基本操作 Ⅱ操作序列 Ⅲ输出 Ⅳ一个特殊情况 四.AC代码 最后 一.读题 ...

  2. POJ 3414 Pots【BFS】+ Python

    原题链接: 3414 -- Pots 参考资料:POJ 3414 - Pots | 眈眈探求 POJ 3414 Pots[BFS][图搜] - it610.com 一 特别注意: 1. 每一种操作对应 ...

  3. 【枚举】【二分答案】【分块答案】【BFS】【最大流】【Dinic】bzoj1189 [HNOI2007]紧急疏散evacuate...

    [法一]枚举Time(0~N*M): S->'.'(1); 'D'->T(Time); '.'->'D'(dis(用BFS预处理,注意一旦到达'D',BFS就不能继续扩展了,注意di ...

  4. nyoj 284 坦克大战【bfs】

    坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" i ...

  5. [kuangbin]专题二 搜索进阶 Escape HDU - 3533【BFS】

    [题目描述] The students of the HEU are maneuvering for their military training. The red army and the blu ...

  6. 【BFS】献给阿尔吉侬的花束(C++)

    [题目描述] 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪.现在研究员们想 ...

  7. 【BFS】天棋哥哥大战AlphaGo 校OJ2395

    题目描述 3月15日,人机围棋大战巅峰对决在韩国首尔落下帷幕.五番棋的最后一局中,韩国著名棋手李世乭尽管与人工智能"AlphaGo"缠斗至官子阶段,但在双双进入读秒后最终还是投子认 ...

  8. POJ3278 HDU2717 Catch That Cow【BFS】

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 100475   Accepted: 31438 ...

  9. 【bfs】WJ的逃离

    WJ(J)的逃离 题目大意: 有一个n×m的矩阵,*是不可走的,0是可走的,求1,1到n,m的最小转弯次数 原题: 题目描述 当WJ醒来时,发现自己被困在一个地图的左上角,幸好WJ有张图,并了解到出口 ...

最新文章

  1. python字典实现关键字检索_如何实现搜索框的关键词提示功能
  2. Java 8 的 JVM 有多快?Fork-Join 性能基准测试
  3. 在CDI应用程序中使用@Alternative
  4. android 字体描边实现,android文字描边功能的实现
  5. RGB与YUV格式简介
  6. android 版本更新工具类_报表分析工具FastReport .Net 2021年超大版本更新,实现了对.NET 5的支持...
  7. canal+Kafka实现mysql与redis数据同步
  8. OOP in PHP
  9. python带我起飞 百度云_CentOS/Debian安装人人影视客户端,下载资源并自动上传到OneDrive网盘...
  10. Java练习21:递归方法求n!
  11. 必学:入行电商产品经理必备知识,原来这么简单
  12. 2012服务器系统如何备份,windows server 2012 r2 如何进行系统备份?
  13. MEDICI仿真NMOS器件晶体管语法笔记
  14. 民企员工股权激励的前提条件
  15. GLUT, freeGLUT, GLFW, GLEW, GLAD 关系与区别
  16. 现在热床寄到了,现在我将热床安装到3D打印机上 --- 3D打印机的底盘校正
  17. 熊瞎子错把虎斑猫看成老虎
  18. java线程的停止,暂停,恢复*
  19. qq企鹅图标java源代码_腾讯QQ更换新标识续:看一个企鹅的蜕变之路
  20. 流程图GoJS用于HTML图表的JavaScript库

热门文章

  1. 月工资5000元,如何快速积累30万?
  2. 爱是谋杀:读徐小斌的《别人》
  3. sql 中位数_【PL/SQL 自定义函数】 常用场景
  4. 陕师大计算机专业,陕师大计算机系组合数学试题
  5. linux搭建lnnp_linux主机安装lnmp详细步骤
  6. 彻底搞懂 python 中文乱码问题_彻底搞懂 Python 编码 - sylan215的软件测试技术学习 - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...
  7. yii3正式版什么时候发布_华为mate50pro什么时候发布
  8. python抽象工厂模式_Python设计模式之抽象工厂模式
  9. GPU Gems1 - 8 衍射的模拟
  10. 【写作】Texlive和Texmaker学习