Catch That Cow

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 70652   Accepted: 22216

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 X - 1 or X + 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.
/*
*题意:有一个农民和一头牛,他们在一个数轴上,牛在k位置保持不动,农户开始时在n位置。
*设农户当前在M位置,每次移动时有三种选择:
*1.移动到M-1;
*2.移动到M+1位置;
*3.移动到M*2的位置。
*问最少移动多少次可以移动到牛所在的位置。所以可以用广搜来搜索这三个状态,直到搜索到牛所在的位置。
*
*/#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;const int N = 200100;
int n, k;
struct node
{int x, step;
};
queue<node> Q;
int vis[N];void BFS()
{   //搜索关于X的三种状态int X, STEP;while(!Q.empty()){node tmp = Q.front();Q.pop();X = tmp.x;STEP = tmp.step;if(X == k){printf("%d\n",STEP);return ;}if(X >= 1 && !vis[X - 1]) //要保证减1后有意义,所以要X >= 1{node temp;vis[X - 1] = 1;temp.x = X - 1;temp.step = STEP + 1;Q.push(temp);}if(X <= k && !vis[X + 1]){node temp;vis[X + 1] = 1;temp.x = X + 1;temp.step = STEP + 1;Q.push(temp);}if(X <= k && !vis[X * 2]){node temp;vis[X * 2] = 1;temp.x = 2 * X;temp.step = STEP + 1;Q.push(temp);}}
}int main()
{while(~scanf("%d%d",&n,&k)){while(!Q.empty()) Q.pop();memset(vis,0,sizeof(vis));vis[n] = 1;node t;t.x = n, t.step = 0;Q.push(t);BFS();}return 0;
}

POJ3278Catch That Cow(BFS)相关推荐

  1. POJ 3278 Catch That Cow(BFS)

    题目网址:http://poj.org/problem?id=3278 题目: Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  2. hdoj2717Catch That Cow(BFS)

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

  3. HDU 2717 Catch That Cow(BFS)

    题目链接 好裸,BFS.杭电多组..2A.. 1 #include <stdio.h> 2 #include <string.h> 3 int p[100001],o[1000 ...

  4. C - Catch That Cow(BFS)

    找奶牛 有三种移动方式,作为BFS搜索的方向 利用 当 n > k 时只能向后走,优化结果 #include<iostream> #include<cstring> #i ...

  5. poj 3278 Catch That Cow (bfs)

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

  6. HDU-2717-Catch That Cow(bfs)

                                           Catch That Cow Problem Description Farmer John has been infor ...

  7. HDU2717 Catch That Cow(bfs)

    题意:农夫的牛跑了,给出农夫和牛在坐标轴上的位置n和k,农夫每次只能从点n移动到n-1.n+1或者n*2的位置.输出抓到牛所需要的最小移动次数. 思路:思路明显的bfs,每次搜索只按照这三种方式,标记 ...

  8. 搜索 —— 广度优先搜索(BFS)

    [概述] 广度优先搜索从初始状态 S 开始,利用给定的规则,生成当前状态所有可能的状态,构成的下一层节点,检查是否出现目标状态G,若未出现,就对该层所有状态节点,分别顺序利用规则再次生成再下一层的所有 ...

  9. 广度优先算法(BFS)入門理解

    简介 广度优先算法(BFS)和深度优先算法(DFS)是图这种数据结构最基础的算法,所以需要我们花心思去学习.能知道BFS和DFS这个名词就证明同学们已经在书上看到了他们的介绍.不过对于初入门的同学来说 ...

最新文章

  1. Facebook又放大招!开源框架Pythia让深度学习更高效
  2. 【码云周刊第 68 期】数据可视化:商业智能的未来!
  3. 不可思议的Word2Vec系列一数学原理
  4. “AI+”赋能元宇宙,一文探讨智能交互的技术支撑
  5. 互联网晚报 | 3月2日 星期三 |​ ​最高法:电商不得以商品已拆封为由拒绝七日无理由退货;小米投资纽迪瑞...
  6. 10.2829(NOIP模拟修正总结)
  7. C++socket编程(七):7.3 http的响应协议
  8. 苹果开发者账户需要同意并添加电话号码,苹果账号忘记验证问题解决方案
  9. json 插入数据_MongoDB如何一次插入多条json数据
  10. mysql 版本_mysql各个版本介绍
  11. java jdk 8 中文文档
  12. window.dialogArguments.location.reload();
  13. linux 安装pgadmin4
  14. 经济机器是如何运行的
  15. 初出茅庐的小李第23篇博客之WiFi模块建立TCP通信并获取B站粉丝数据
  16. 字符编码常识及问题解析
  17. (七)通过pygame来设置飞机大战中 敌机 的速度、位置等
  18. 瞧瞧你的指甲,看你身体还好不!
  19. 一个基于EntityFrameworkCore+Lucene实现的全文搜索引擎库
  20. Day9--MATLAB常用绘图命令

热门文章

  1. 全流程解读:建立销售和营销模型的 9 个步骤
  2. 三家逐鹿,私有化部署能帮神策数据杀出重围么?| 公司调研
  3. 大数据和人工智能的关系,超全解析
  4. MySQL探索(一):B-Tree索引
  5. Gson 使用总结 高级用法
  6. sdut 2129树结构练习——判断给定森林中有多少棵树(并查集)
  7. 【转载】不要一辈子靠技术生存
  8. How to become an expert in the IP industry? Here is where you should start
  9. 一个没有好好的适应本土市场的失败的案例
  10. mattermost