Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN (0 ≤N ≤ 100,000) on a number line and the cow is at a pointK (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 orX+ 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 andK
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.

本题思路:

哎我是copy的

主要在于利用队列 这个广搜

c++代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int maxn=100009;
int book[maxn]={0};
int step[maxn]={0};
queue<int>q;
int bfs(int n,int k)
{
    int i;
    int t;
    book[n]=1;
    step[n]=0;
    q.push(n);
    while(q.empty()==0)
    {
        int head=q.front();
        q.pop();
        for(i=1;i<=3;i++)
        {
            if(i==1)
            t=head+1;
            else if(i==2)
            t=head-1;
            else if(i==3)
            t=head*2;
            if(t<0||t>maxn)
            continue;
            if(book[t]==0)
            {
            q.push(t);
            book[t]=1;
            step[t]=step[head]+1;
            }
            if(t==k)    
            return step[t];
        }
    }
}
int main()
{
    int n,k;
    while(~scanf("%d%d",&n,&k))
    {
        memset(book,0,sizeof(book));
           memset(step,0,sizeof(step));
        while(q.empty()==0)q.pop();
        if(n>=k)printf("%d\n",n-k);
        else if(n<k)printf("%d\n",bfs(n,k));
    }
    return 0;
}

B - Catch That Cow(广度搜索)相关推荐

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

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

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

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

  3. 【搜索入门专题1】hdu2717 H - Catch That Cow 【BFS】

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

  4. C - Catch That Cow POJ - 3278

    C - Catch That Cow POJ - 3278 首先是大暴搜+最优化剪枝(当当前搜索情况不如已有答案优时,返回) #include<cstdio> #include<al ...

  5. POJ 3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 30924   Accepted: 9536 D ...

  6. POJ 3278 Catch That Cow(BFS)

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

  7. 动态规划训练13 [Catch That Cow poj3278]

    Catch That Cow POJ - 3278 这道题我看大家用的方法都是bfs搜索,为什么在我看来这就是一个动态规划的题目啊啊啊啊啊啊啊 dp[x]表示从N出发到x所需要的最小时间 那么得到如下 ...

  8. C - Catch That Cow

    这是我第一次遇到的BFS问题,因为要学习编程,F同学帮我找了一些搜索的题目,做到这个问题的时候感觉无法使用DFS来写,因为他可能是个无底洞.因为当时没有学习过BFS,所以网上搜索了下发现了也是一位第一 ...

  9. POJ3278 HDU2717 Catch That Cow【BFS】

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

  10. poj 3278 bzoj 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛(BFS)

    1646: [Usaco2007 Open]Catch That Cow 抓住那只牛 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1223  Solv ...

最新文章

  1. 简便解法:1004 成绩排名 (20分)
  2. oracletns中不存在名称为_oracle tnsname.ora中的SERVICE_NAME 代表实例的名称还是代表全局数据库的名称?...
  3. 面试问题背后的“猫腻”
  4. mysql数据库binlog日志截断报错,导致mysql主从同步失败故障排查
  5. 在继续之前,如何暂停我的Shell脚本一秒钟?
  6. 三刺激值计算公式_超实用的数控车床常用计算公式,绝对帅,赶紧收藏了!
  7. C语言题目——三子棋游戏
  8. Linux内核学习路线 学习linux内核的建议
  9. linux cat命令什么意思
  10. 计算机word2003界面,Word2003工作界面简介
  11. 麒麟子出了一款免费3D角色虚拟摇杆控制器!这也太好用了
  12. 为什么很多人交易十几年都做不到稳定盈利呢?
  13. 校技能节之打铁记~~~~~~
  14. android启动过程中cpu降频,android省电开发之cpu降频
  15. 欧洲为何没有互联网巨头?
  16. 力扣(141.21)补9.1
  17. latex三线表绘制,合并行与列
  18. 年薪40万AI工程师必备的能力项,你达标了吗?
  19. Access to XMLHttpRequest at ‘file:///D:/xx/xxx.json‘ from origin ‘null‘ has been blocked by CORS问题解决
  20. 图像处理(九)-图片和数组的转换

热门文章

  1. python实现卷积神经网络_【455】Python 徒手实现 卷积神经网络 CNN
  2. 微信小程序CSS之Flex布局
  3. [雪峰磁针石博客]软件测试专家工具包2性能测试
  4. 在Android中实现一个简易的Http服务器
  5. 4698. [SDOI2008]Sandy的卡片【后缀数组】
  6. javascript的window.open()具体解释
  7. Eclipse设置智能提示
  8. Hibernate(5)—— 联合主键 、一对一关联关系映射(xml和注解) 和 领域驱动设计...
  9. 深入理解JavaScript内部原理(6): 闭包
  10. 迁移 Linux 系统,第 1 部分——如何迁移备份和裸机恢复 Linux 系统