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.
    题意是:
    给定一个起点,有三种方法能到达终点,分别是:
    1.每次减 1
    2.每次加 1
  1. 每次以乘 2 前进
    求最少的步数(用BFS)
    分为两种情况:
    一个是 n>=k 就只能以第一种的方法向后退,最少步数就是两数的 差
    另一种是 n<=k 的情况
    满足范围即入队列,对当前的坐标进行标记,步数加1;到达目标点返回结果
    代码如下:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXX = 1e5+10;struct NODE
{int x;int step;
}now,pre,t;
int vis[MAXX];
queue<NODE> q;int BFS(int x,int y)
{while(!q.empty())q.pop();pre.x = x;pre.step = 0;q.push(pre);vis[x] = 1;while(!q.empty()){now = q.front();q.pop();for(int i=0; i<=2; i++){if(i == 0)t.x = now.x - 1;else if(i == 1)t.x = now.x + 1;else if(i == 2)t.x = now.x * 2;if(t.x >= 0 && t.x <= 100000 && vis[t.x] == 0){if(t.x == y)return now.step + 1;else {vis[t.x] = 1;t.step = now.step + 1;q.push(t);}}}}}
int main()
{int n,k,ans;memset(vis,0);cin >> n >> k;if(k <= n)cout << n-k << endl;else {ans = BFS(n,k);cout << ans << endl;}
}

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. Catch That Cow【广搜】

    Catch That Cow POJ - 3278 Farmer John has been informed of the location of a fugitive cow and wants ...

  6. poj 3278 Catch That Cow(广搜)

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

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

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

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

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

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

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

  10. 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设备驱动模型和Kobject
  2. hadoop 集群:物理机突然间ping不同虚拟机
  3. 请问华为三层交换机里面的那个从IP是个什么意思? -
  4. 10月末周网络安全报告:应用程序漏洞占比93.1%
  5. WPF中显示任意目录的图片或其他资源文件
  6. css sprites css精灵
  7. Web前端-Vue.js必备框架(一)
  8. Futureswap公布代币分配时间表,在未来3年向用户分配3000万枚FST代币
  9. iOS实现文字渐变效果
  10. Linux下Socket通信(TCP实现)
  11. 域服务器安全策略应用,ad域服务器组策略命令
  12. SpringCloud之熔断器Hystrix
  13. java monitor怎么使用_Java monitor机制使用方法解析
  14. 读书笔记_稻盛和夫《心》
  15. 特征工程(三) 数据标准化和归一化
  16. 洛谷 P5594 【XR-4】模拟赛
  17. vs2017下libcef配置
  18. 为什么父类引用可以指向子类对象 子类引用不能指向父类对象 泛型
  19. 基于计算机视觉的无人驾驶感知系统
  20. 修改的屏幕保护-码农的梦

热门文章

  1. git 版本回退
  2. C语言编程计算差商表,计算方法C语言编程计算方法C语言编程.doc
  3. 【Windows下设置全局以管理员身份运行cmd】
  4. 《Graph Learning》| HIN-基于元路径的相似度
  5. 精读《Excel JS API》
  6. Linux安全增强技术,采用MAC技术的嵌入式Linux安全增强技术研究.pdf
  7. 未能加载文件或程序集 System Drawing Version 4 0 0 0 Culture neutral
  8. 作为一个面试官,我会这样问问题——面试攻略之见招拆招
  9. Android JNI之pthread_create的失败(1)
  10. 码代码时遇到的小插曲