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. Apache2.4整合tomcat8

    1,编译mod_jk.so 不同版本apache对应不同的mod_jk,试验用tomcat-connectors-1.2.41-src.tar.gz , 下载后,解压,进入./native文件夹依次输 ...

  2. PHP 面向对象及Mediawiki 框架分析(二)

    mediaHandler可以理解为处理media文件的 /includes/filerepo/file/File.php /** * Get a MediaHandler instance for t ...

  3. 后向传播算法“backpropragation”详解

    为什么要使用backpropagation? 梯度下降不用多说,如果不清楚的可以参考梯度下降算法. 神经网络的参数集合theta,包括超级多组weight和bais. 要使用梯度下降,就需要计算每一个 ...

  4. strspn() 和 strcspn() 函数【转】

    本文转载自:https://flyer103.wordpress.com/2011/06/03/strspn-%E5%92%8C-strcspn-%E5%87%BD%E6%95%B0/ 前几天在看一本 ...

  5. Linux挂载第二块硬盘操作方法

    远程SSH登录上Centos服务器后,进行如下操作 提醒:挂载操作会清空数据,请确认挂载盘无数据或者未使用 第一步:列出所有已挂载磁盘 命令: disk -h [root@gluster_node1 ...

  6. 导入Jquery.min.js时 JQuery 上打红X了

    问题解决:右击jquery.min.js——>MyEclipse——>点击Exclude From Validation——>点击Run Validation 即可

  7. JavaWeb -- 邮件收发

    smtp协议: telnet smtp.qq.com 25 ehlo kevin auth login eGlhbmdqaWU1NUBxcS5jb20= a2V2aW5feGlhbmc1NQ== ma ...

  8. CSS3中与文字相关的样式

    1.给文字添加阴影:text-shadow属性(特别指出IE浏览器要IE10+的版本才支持)    语法如下: text-shadow:length length length color; 其中,第 ...

  9. Linux软件安装(一)

    软件安装方式: 1. rpm方式 该方式软件安装本质与Windows下安装软件方式一致,就是把软件包里面的二进制代码文件复制到系统指定目录(如 C://program files) 优点:软件安装非常 ...

  10. SQL 存在一个表而不在另一个表中的数据

    原文链接:http://blog.csdn.net/windren06/article/details/8188136 (转)A.B两表,找出ID字段中,存在A表,但是不存在B表的数据.A表总共13w ...