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

                                                                                 Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 47010   Accepted: 14766

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.

数组必须开大点

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> using namespace std;
struct node
{
int x,ans;
} q[];
int jx[]= {-,};
int n,k;
int map[],v[];
void bfs()
{
struct node t,f;
int e=,s=;
t.x=n;
v[t.x]=;
t.ans=;
q[e++]=t;
while(s<e)
{
t=q[s++];
if(t.x==k)
{
printf("%d\n",t.ans);
break;
}
for(int i=; i<; i++)
{
if(i==)
f.x=t.x*;
else f.x=t.x+jx[i];
if(!v[f.x]&&f.x>=&&f.x<=)
{
f.ans=t.ans+;
q[e++]=f;
v[f.x]=;
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(map,,sizeof(map));
memset(v,,sizeof(v));
bfs();
}
return ;
}

poj3278 Catch That Cow(简单的一维bfs)的更多相关文章

  1. POJ3278——Catch That Cow(BFS)

    Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...

  2. POJ3278 Catch That Cow —— BFS

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  3. poj3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 73973   Accepted: 23308 ...

  4. POJ3278——Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 114140   Accepted: 35715 ...

  5. 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)

    链接:https://ac.nowcoder.com/acm/contest/984/L 来源:牛客网 Catch That Cow 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3 ...

  6. POJ3278 Catch That Cow(BFS)

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

  7. POJ 3278 Catch That Cow(简单BFS)

    题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次 ...

  8. poj-3278 catch that cow(搜索题)

    题目描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...

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

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

随机推荐

  1. Orleans学习总结(一)--入门认识

    最近这段时间接触了些新的东西:Orleans框架.今天是春节前最后一天班,把我这段时间学习的东西总结一下分享给大家. 一.什么是Orleans (文档地址.这里我就直接翻译官方的介绍,有点地方翻译的有 ...

  2. 关于pyinstall打包时的依赖问题

    前几天写了一个人脸表情分析的小程序,想用pyinstall打包成一个可以移植的小软件.因为之前用过pyinstall,所以这次使用同样的方法对我的程序进行打包: [pyinstaller -F --i ...

  3. Scriter CSS

    transition: height(quart-out,1.0s,quart-in); transform:rotate(50deg); http://www.terrainformatica.co ...

  4. AJAX基本操作 + 登录 + 删除 + 模糊查询

    AJAX练习显示页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...

  5. 在PHP系统里连接MySQL 数据访问,+ + + + + 数据删除

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. ERP项目实施记录02

    今天去第三方公司(B公司)考察: 公司成立:2011年12月 注册地:深圳 深圳:2~3个业务员 东莞:5个开发人员,据说也是实施人员 全功能者:BOSS A公司因战略调整,要将业务"下放& ...

  7. MySql 远程连接的条件

    1.首先看服务器防火墙 引用:http://www.cnblogs.com/silent2012/archive/2015/07/28/4682770.html CentOS 7.0默认使用的是fir ...

  8. ubuntu 搜狗输入法 在中断失效

    实测是更换了皮肤后,出现在中断输入故障,乱码.在其他界面可能是正常的,更换语言没用. killall fcitx输入法突然好了(根据网友所说,更改一堆东西貌似并没有什么用) 此时关闭输入法皮肤一切正常 ...

  9. [No0000163]卷福、神秘博士和一群老戏骨表演群口相声:To be or not to be该咋念,简直高潮迭起

        'To be or not to be, that is the question',<哈姆雷特>中这句经典台词到底应该怎么念? 这是古今无数哈姆雷特演员最爱琢磨的问题,一千个人就 ...

  10. 安装arcgis10.5不能启动服务的解决方案

    最近由于公司需要,要装arcgis10.5,但是装这软件就费了好久的功夫.以前用的10.2,安装比较简单,但是10.5看起来就不一样了,下载完成后就会发现多了一个破解文件.按照教程一步一步安装的,但是 ...