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. VS 2013 未找到与约束contractname Microsoft.VisualStudio.Utilities.IContentTypeRegistryService......匹配的导出

    错误信息: 点击解决方案中的文件,就会提示这个错误.最近给vs2013安装了python的插件,安装了vs2017的python,可能是这两个导致了这个错误. 解决方案: 删除 C:\Users\ji ...

  2. DOM内容操作

    <table border="2"> <thead id="1" class="c1 c2"> <tr> ...

  3. QQ音乐flac音乐转MP6格式怎样实现

    很多时候我们所下载的音乐格式都不是MP3格式的,用起来都是有局限性的,因为很多播放器都是支持MP3格式的.很多时候为了方便使用,我们就需要将不同的音乐格式转换为MP3格式的.如flac音乐转MP3的问 ...

  4. A股行情记录

    14年:券商 + 高铁 15年:大牛 16年:钢铁,煤炭(去产能) 17年:消费,大白马(去杠杆) 18年:科技股,垃圾股(股权质押),科创板 19年:券商 + 大金融 + 科技股 ?

  5. export,import ,export default区别

    export,import ,export default区别 一.export,import ,export default ES6模块主要有两个功能:export和import export用于对 ...

  6. Django:模板template(二)

    将跨站请求伪造和验证码的东西记一下 CSRF Cross Site Request Forgery.跨站请求伪造 链接:GET请求:表单:POST请求 某些恶意的网站上,包含链接.表单.按钮.Java ...

  7. LCA&最小生成树

    LCA 经常被用来使用.比如询问树两点之间的距离. 比如树上差分 都是经常被使用的类型.有的时候倍增求LCA的同时还可以优化算法. 这道题呢 求一个严格的最小生成树,当然如果不严格的话如果有重边那么就 ...

  8. 阿里创新自动化测试工具平台--Doom

    摘要: 阿里内部诞生一了个依赖真实流量用于自动回归的自动化测试平台,通过创新的自动mock机制不仅支持读接口的回归验证,同时支持了写接口验证,在内部产生了极大价值,有价值的东西就应该分享,目前该工具已 ...

  9. PLSQL分级取数据

    分级取数据 select employee_id,last_name,job_id,manager_id from employees start with employee_id=101 --emp ...

  10. MonkeyRunner_模拟机_运行脚本

    1.打开创建好的Android模拟机  (使用AVD Manager.exe打开,或者使用cmd窗口 emulator -avd test2打开) 2.打开cmd窗口,输入monkeyrunner,然 ...