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 X - 1 or
X + 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.





题意:在一维的直线上,给出John的位置n,和cow的位置k,求出John按照给定三种规则找到cow的最少步数。

分析:用广度优先搜索从源点开始,依次用三种规则进行查找,设置边界条件,最先找到的即为最优解。这里还要注意下数组边界大小要比用于比较的边界要大一点,否则会数组越界而RE,我在这里吃了6个RE,一直没找到原因。后来才惊奇地发现时用于比较的MAX和数组大小Max相同。

import java.util.LinkedList;
import java.util.Scanner; public class Main {
static int start, end;
static int MAX = 200000;
static LinkedList<Integer> q;
static boolean[] visited;
static int[] step;
static int t, next; static int bfs() { q.add(start);
visited[start] = true;
step[start] = 0; while (!q.isEmpty()) { t = q.poll(); for (int i = 0; i < 3; i++) { if (i == 0) {
next = t - 1;
} else if (i == 1) {
next = t + 1;
} else {
next = t * 2;
} if (next > MAX || next < 0)
continue; if (!visited[next]) {
q.add(next);
step[next] = step[t] + 1;
visited[next] = true;
} if (next == end)
return step[next];
}
}
return -1;
} public static void main(String[] args) {
Scanner sc = new Scanner(System.in); q = new LinkedList<Integer>();
//在这个地方比MAX多加上5,放在RE
visited = new boolean[MAX+5];
step = new int[MAX+5]; start = sc.nextInt();
end = sc.nextInt(); if (start >= end) {
System.out.println(start - end);
} else {
System.out.println(bfs());
} }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Poj 3287 Catch That Cow(BFS)的更多相关文章

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

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

  2. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

  3. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  4. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  5. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  6. poj 3278 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+队列)

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

  8. POJ - 3278 Catch That Cow bfs 线性

    #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...

  9. POJ 3278 Catch That Cow bfs 难度:1

    http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...

随机推荐

  1. iOS_网络编程

    网络编程中有以下几种方式向服务器进行提交数据: IOS同步请求.异步请求.GET请求.POST请求 1.同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可 ...

  2. 高通LCD驱动调试

    本文转载自:http://www.itgo.me/a/x6305658852004979994/lcd%20qcom 来自 :http://blog.csdn.net/dacaozuo/article ...

  3. Go 内置库 IO interface

    基本的 IO 接口 io 包为 I/O 原语提供了基本的接口.它主要包装了这些原语的已有实现. 由于这些接口和原语以不同的实现包装了低级操作,因此除非另行通知,否则客户端不应假定它们对于并行执行是安全 ...

  4. Docker 数据收集利器:cadvisor

    gitHub地址:https://github.com/google/cadvisor cAdvisor cAdvisor (Container Advisor) provides container ...

  5. win10系统下载地址

    Win10正式版微软官方原版ISO系统镜像下载: Win10正式版32位简体中文版(含家庭版.专业版) 文件名: cn_windows_10_multiple_editions_x86_dvd_684 ...

  6. 语义web相关概念

    前言:最近做的项目是自然语言处理相关的,看了一本书<语义web技术基础>,总的来看,接触自然语言处理,语义理解也有差不多一年的时间了.这两天想了一想,自己究竟学到了什么,掌握了哪些新的知识 ...

  7. iOS APP AppIcon& LaunchImage

    AppIcon size for iPhone: 29 - Settings @1x 29*29,  58 - Settings @2x 58*58, 87 - Settings @3x 87*87 ...

  8. 【转】服务器.htaccess 详解以及 .htaccess 参数说明

    htaccess文件(或者”分布式配置文件”)提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目录及其所有子目录.作为用户,所能使用的命令受到限 ...

  9. UOJ12 猜数

    这一天,小Y.小D.小C正在愉快地玩耍. 小Y是个数学家,他一拍脑袋冒出了一个神奇的完全平方数 nn. 小D是个机灵鬼,很快从小Y嘴里套出了 nn 的值.然后在脑内把 nn 写成了 a×ba×b的形式 ...

  10. 使用 Spring Boot 快速构建 Spring 框架应用

    Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2002 年发布以来,Spring 框架已经成为企业应用开发领域非常流行的基础框架.有大量的企业应用基于 Spring 框架来开发.S ...