链接:

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

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 62113   Accepted: 19441

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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue> using namespace std; #define N 110000 struct node
{
int x, step;
}; int s, e;
bool vis[N]; int BFS(int s)
{
node p, q;
p.x = s, p.step = ; memset(vis, false, sizeof(vis));
vis[s] = true;
queue<node>Q;
Q.push(p); while(Q.size())
{
p = Q.front(), Q.pop(); if(p.x == e) return p.step; for(int i=; i<; i++)
{
if(i==)
q.x = p.x + ;
else if(i==)
q.x = p.x - ;
else if(i==)
q.x = p.x * ; q.step = p.step + ;
if(q.x>= && q.x<N && !vis[q.x])
{
Q.push(q);
vis[q.x] = true;
}
}
} return -;
} int main()
{
while(scanf("%d%d", &s, &e)!=EOF)
{
int ans = BFS(s); printf("%d\n", ans);
}
return ;
}

(广搜)Catch That Cow -- poj -- 3278的更多相关文章

  1. catch that cow POJ 3278 搜索

    catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...

  2. Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索

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

  3. Catch That Cow POJ - 3278 bfs map超时,短路判断顺序。

    题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数. 坑:标题写了.有点不会写bfs了... ac代码 #define _CRT_SECURE_NO_WARNINGS #include& ...

  4. C - Catch That Cow POJ - 3278

    //标准bfs #include <iostream> #include <cstdio> #include <algorithm> #include <cm ...

  5. kuangbin专题 专题一 简单搜索 Catch That Cow POJ - 3278

    题目链接:https://vjudge.net/problem/POJ-3278 题意:人可以左移动一格,右移动一格,或者移动到当前位置两倍下标的格子 思路:把题意的三种情况跑bfs,第一个到达目的地 ...

  6. (广搜)Dungeon Master -- poj -- 2251

    链接: http://poj.org/problem?id=2251 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2137 ...

  7. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  8. poj 3278 Catch That Cow (广搜,简单)

    题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...

  9. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

随机推荐

  1. Application共享数据

    1.Application与Session的区别 Application对象:实现程序级别的数据共享. Session对象:实现会话级别的数据共享. 当需要整个程序级别的共享信息时,可以使用Appli ...

  2. oracle 表空间总结

                           表空间总结  一.认识表空间 1:表空间概念: 表空间是数据库中最大的逻辑单位,Oracle数据库采用表空间将相关的逻辑组件组合在一起,一个Oracle数 ...

  3. Form中的keypress事件不能用

    Form中的keypress事件不能用 编写人:CC阿爸 2015-4-8 近期在修改系统时,想给一画面增加一个组合键功能,但在form_keypress事件中加入代码,但无论如何也不能触发该动作. ...

  4. Redis队列——PHP操作简单示例

    入队操作 <?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); while(True){ try{ $value = ...

  5. 汇编_指令_REP MOVESB 和 CLD

    先说说MOVSB(MOVe String Byte):即字符串传送指令,这条指令按字节传送数据.通过SI和DI这两个寄存器控制字符串的源地址和目标地址,比如DS:SI这段地址的N个字节复制到ES:DI ...

  6. IDEA中修改各个部位的字体大小

    1.菜单栏 Setting -> Appearance&Behavior -> Appearance ->Override default fonts by (not rec ...

  7. SOLR对多个(关联)表创建索引

    又两天没写博客,关于SOLR的东西,写了一周了还没写完我也是醉了,毕竟会的东西真不多,周四晚上加班没写,周五晚上公司同事聚会也没写,今天在家,还是把最后的一点写完吧,我会的剩下的也就是一个对多个表创建 ...

  8. git/github基本命令

    Git与项目 git的使用,主要包括: 本地仓库的命令 远程仓库的命令 项目需求.页面.模型类的设计,及页面的使用 sudo apt-get install git 安装成功后,运行如下命令 git ...

  9. go run helper

    # go run helper -a :强制编译相关代码,不论编译代码是否最新 -n :打印编译过程需要用到的命令,但不真正执行他们 -p n :并行编译,n为并行的数量 -v :列出被编译的代码包的 ...

  10. Go语言优势与劣势

    Go语言的优势: 1.脚本化的语法:开发效率高,容易上手 2.静态类型+编译型,程序运行速度有保障:静态类型+编译型语言相对于动态类型+解释型语言的效率高 3.原生的支持并发编程:降低开发.维护成本/ ...