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. linux如何安装多个jdk?

    1 官网下载JDK版本 jdk-8u181-linux-x64.tar.gz 2 利用ssh工具上传安装包到Linux系统 传至:/usr/local 3 Linux用户安装的程序一般放在 /usr/ ...

  2. Flask web开发之路十一

    首先写一下cookie和session的概念,然后是Flask中session的工作机制以及操作session ### cookie: 1. `cookie`出现的原因:在网站中,http请求是无状态 ...

  3. TOP100summit2017:网易测试总监钱蓓蕾——新时代测试正走向精英化、自动化、智能化

    壹佰案例:钱蓓蕾老师您好,很荣幸邀请到您成为第六届壹佰案例峰会测试专场的联席主席.您从事测试工作11年了,您觉得最近几年测试工作有什么趋势上的变化么? 钱蓓蕾:测试行业近几年变化挺大的,正逐渐向精英化 ...

  4. vue中的iviewUI导出1W条列表数据每次只导出2000条的逻辑

    导出弹窗的html <template> <Modal v-model="exportModal" width=400 :closable="false ...

  5. Codeforces 977F - Consecutive Subsequence - [map优化DP]

    题目链接:http://codeforces.com/problemset/problem/977/F 题意: 给定一个长度为 $n$ 的整数序列 $a[1 \sim n]$,要求你找到一个它最长的一 ...

  6. C#配置.INI文件

    百度搜了一些资料,好多没给示例,只给了代码.让人看了直接懵逼,后来找了个靠谱的:http://www.jb51.net/article/118591.htm

  7. strlen函数的汇编实现分析

    [原创][老刘谈算法001]这位运算玩的真溜—strlen函数的汇编实现分析-『密码算法』-看雪安全论坛 https://bbs.pediy.com/thread-229243.htm

  8. JsLint 的安装和使用

    JSLint 是一款Javascript验证工具,在定位错误并确保基本指南得以遵循时,非常有用.如果你正在编写专业级的javascript,应该使用 JSLint 或者类似的验证工具(JSHint). ...

  9. Altium Designer 绘图流程及快捷键

    1.Shift+Ctrl+g 设置栅格捕捉大小 2.Q 切换单位 3.E+N +点击字体 改变字体大小 4.自动布线前需在Mechanical 层和keepout层添加一个边框 5.打过孔实现双面走线 ...

  10. jquery基础学习之DOM篇(二)

    在此之前请牢记,jquery 是一个合集对象!!!! 1.节点创建 js创建方法: 创建元素:document.createElement 设置属性:setAttribute 添加文本:innerHT ...