题目:

                                                                                                                                                         Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
     

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

分析:题目大意是在数轴上给定任意起点n、终点k,对任意的点坐标x每次有3种走法:x-1,x+1,2*x。每走一次花费时间为1minute,问从n到k最少需要花费多少时间?

      该题是最短路径问题,于是可以用BFS搜索,每次往三个方向BFS,直到到达k,每走一步记录当前时间。

//simonPR
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=;
int n,k,vis[maxn];
struct catche{
int site,times; //记录点和所花的时间。
};
queue<catche> q;
void init();
void init(){
while(!q.empty())
q.pop();
memset(vis,,sizeof(vis));
}
int bfs(int n,int k);
int bfs(int n,int k){
if(n==k) return ;
catche now,news,start;
start.site=n;
start.times=;
q.push(start); //将起点入队列。
vis[n]=;
while(!q.empty()){
int ts;
now=q.front();
q.pop();
for(int i=;i<;i++){ //分三个方向BFS。
if(i==) ts=now.site-;
else if(i==) ts=now.site+;
else ts=*now.site;
if(ts>maxn||vis[ts]==) continue; //如果已经访问过了或超出数据范围则跳过。
if(ts==k) return now.times+; //到达终点K,返回时间。
if(vis[ts]==) //更新点信息,并将新点入队列。
{
vis[ts]=;
news.site=ts;
news.times=now.times+;
q.push(news);
}
}
}
}
int main()
{
scanf("%d%d",&n,&k);
init(); //初始化。
int ans=bfs(n,k);
printf("%d\n",ans);
return ;
}
												

POJ-3278(BFS)的更多相关文章

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

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

  2. 【BFS】POJ 3278

    POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最 ...

  3. BFS POJ 3278 Catch That Cow

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

  4. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

  5. catch that cow POJ 3278 搜索

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

  6. [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)

    BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...

  7. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...

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

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

  9. POJ 3278 Catch That Cow(模板——BFS)

    题目链接:http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a f ...

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

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

随机推荐

  1. android Spinner的使用

    首先是MainActivity package com.example.spinnertest; import java.util.ArrayList; import java.util.List; ...

  2. C# Read/Write another Process' Memory ZZ

    Today's tutorial is about...processes' memory! In this article I'll show you how to read/write a pro ...

  3. java 正则表达式例子, 查找字符串

    import java.util.regex.Matcher;import java.util.regex.Pattern; public class Main { public static voi ...

  4. 参考SQLHelper编写的OracleHelper

    使用 Oracle.ManagedDataAccess.Client 类库参考SQLHelper编写的OracleHelper: // ================================ ...

  5. 学习手工创建表,表关系以及用exists 来查询

    ---创建表a If exists(select * from sysobject where [name]=='a' and xType = 'u') Begin Drop table aa End ...

  6. Web---创建Servlet的3种方式、简单的用户注册功能

    说明: 创建Servlet的方式,在上篇博客中,已经用了方式1(实现Servlet接口),接下来本节讲的是另外2种方式. 上篇博客地址:http://blog.csdn.net/qq_26525215 ...

  7. GPGPU OpenCL/CUDA 高性能编程的10大注意事项

    转载自:http://hc.csdn.net/contents/content_details?type=1&id=341 1.展开循环 如果提前知道了循环的次数,可以进行循环展开,这样省去了 ...

  8. C语言练习题_邮票组合

    背景:        我们寄信都要贴邮票,在邮局有一些小面值的邮票,通过这些小面值邮票中的一张或几张的组合,可以满足不同邮件的不同的邮资.        现在,邮局有4种不同面值的邮票.在每个信封上最 ...

  9. 操作12864(ST7920控制器)

    引脚部分查看中文的12864介绍,下面这些可以在ST7920的英文数据手册里查到. Function Description 部分介绍工作方式.存储器.操作方法.Instructions 部分介绍指令 ...

  10. 字符编码笔记:ASCII,Unicode和UTF-8,附带 Little endian和Big endian的解释

    作者: 阮一峰 日期: 2007年10月28日 今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个问题比我想象的复杂,从午饭后一直看到晚上9点,才算初步 ...