Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 32071   Accepted: 9866

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

代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
using namespace std;
int n,k;
queue<int>Q;
#define N 100005
int visit[N];
int num[N];

int BFS(int x)
{
   Q.push(x);
   visit[x]=1;
   int a,b;
   while(!Q.empty())
   {
    a=Q.front();
    Q.pop();
    for(int i=0;i<3;i++)
    {
       if(i==0)
    b=a+1;
    else if(i==1)
    b=a-1;
    else
    b=2*a;
    if(b<0||b>N)
    continue;
    if(!visit[b])
    {
    Q.push(b);
    visit[b]=1;
    num[b]=num[a]+1;     
    if(b==k)
    return num[b];
    }   
    }    
   }  
}

int  main()
{
  scanf("%d%d",&n,&k);
  memset(num,0,sizeof(num)); 
  memset(visit,0,sizeof(visit));
  if(n>=k)
  printf("%d\n",n-k);
  else
  printf("%d\n",BFS(n));
  //system("pause");
  return 0;
}

链接:http://poj.org/problem?id=3278

转载于:https://www.cnblogs.com/hebozi/archive/2012/09/12/2682278.html

POJ 3278 Catch That Cow BFS相关推荐

  1. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  2. BFS POJ 3278 Catch That Cow

    题目传送门 1 /* 2 BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 3 */ 4 #include <cstdio> 5 #include <iostrea ...

  3. POJ 3278 Catch That Cow

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

  4. POJ 3278 Catch That Cow(BFS)

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

  5. bfs+dfs分析----poj 3278 Catch That Cow

    题目详情 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 115430   Accepted:  ...

  6. POJ 3287 Catch That Cow(bfs)

          看懂意思就很简单了,给你一个数,每次只能加一或者减一或者乘2,问最少几次才能变成另一个数,就是一个简单的bfs过程,看代码吧. AC代码: #include <iostream> ...

  7. POJ 3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35043   Accepted: 10800 ...

  8. poj 3278 Catch That Cow(广搜)

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

  9. poj 3278 Catch That Cow 广搜

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

最新文章

  1. html中js定义数组,javascript数组的定义及操作实例
  2. linux 死锁介绍与简单分析
  3. Html和CSS的关系
  4. Netty ObjectPool对象池技术原理分析
  5. dos命令行说adb不是系统命令
  6. [SpringSecurity]web权限方案_用户认证_查询数据库完成认证
  7. 使用Maven Archetype生成工程报错的解决
  8. MySQL 5.5 手册下载
  9. Web前端基础知识整理
  10. Linux stat命令和AIX istat命令 (查看文件修改时间)
  11. I350系列服务器网卡驱动,Intel英特尔网卡驱动
  12. 阿里云国际版账号登录不上去,账号被风控怎么办?
  13. Excel如何快速删除指定区域公式保留数值
  14. [bzoj3998]弦论
  15. 手Q游戏中心的个性化推荐实战 | CSDN博文精选
  16. 【无标题】体验scratch海底世界
  17. YouTube营销活动方案
  18. jsp中文传值到java乱码_jsp传递参数中文乱码解决办法
  19. ROS中map、odom、base_link坐标系的理解和这三个坐标系在AMCL中的关系
  20. 回溯法经典算法 求集合中所有的子集

热门文章

  1. 计算机应用基础是高中吗,职业高中学业水平测试计算机应用基础试卷(A)及答案...
  2. svm多分类_人工智能科普|机器学习重点知识——支持向量机SVM
  3. sql server排序慢_用Nginx实现接口慢查询并可示化展示TOP 20
  4. flowable工作流_【程序源代码】Springboot开源工作流开发框架
  5. idea启动springboot卡_写给新手看的 Spring Boot 入门学习指南
  6. 网易java二面_网易Java开发面试:一面+二面+三面以及 面试经验总结
  7. PHP中用编码代码,编码对照表的使用(2)_php
  8. Matlab读取二进制文件----fread
  9. 粽子机器人视频教程_包粽子服务机器人的制作方法
  10. C++知识点54——RTTI(运行时类型识别)