Poj 3287 Catch That Cow(BFS)
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
K
Output
Sample Input
5 17
Sample Output
4
Hint
题意:在一维的直线上,给出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)的更多相关文章
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- 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 ...
- 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 ...
- 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 ...
- POJ - 3278 Catch That Cow bfs 线性
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...
- POJ 3278 Catch That Cow bfs 难度:1
http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...
随机推荐
- DNS 缓存机制原理
DNS 缓存机制原理 简单来说,一条域名的DNS记录会在本地有两种缓存:浏览器缓存和操作系统(OS)缓存.在浏览器中访问的时候,会优先访问浏览器缓存, 如果未命中则访问OS缓存,最后再访问DNS服务器 ...
- ResourceLoader笔记
Ant路径匹配 Ant路径通配符支持“?”.“*”.“**”,注意通配符匹配不包括目录分隔符“/”: “?”:匹配一个字符,如“config?.xml”将匹配“config1.xml”: “*”:匹配 ...
- 20145230《java程序设计》第6周学习总结
20145230 <Java程序设计>第6周学习总结 教材学习内容 串流设计的概念 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象.如果要将数据从来源取出,可以 ...
- MySQL之——提示"mysql deamon failed to start"错误的解决方法
网站突然连接不上数据库,于是直接重启了一下服务器.进到cli模式下,执行 service myqsld start 发现还是提示"mysql deamon failed to start&q ...
- HDFS数据复本存放
复本怎么放Hadoop的默认布局策略是在运行客户端的节点上放第一个复本(如果客户端运行在容器之外,就随机选择一个节点,不过系统会避免挑选那些存储太满或太忙的节点).第二个复本放在与第一个不通且随机另外 ...
- 80X86寄存器详解<转载>
引子 打算写几篇稍近底层或者说是基础的博文,浅要介绍或者说是回顾一些基础知识, 自然,还是得从最基础的开始,那就从汇编语言开刀吧, 从汇编语言开刀的话,我们必须还先要了解一些其他东西, 像 CPU ...
- Spring初学之泛型依赖注入
主要讲泛型依赖注入,所以核心在java文件,配置文件中只需配置扫描包即可,如下: <?xml version="1.0" encoding="UTF-8" ...
- Java执行过程
Java的运行原理 在Java中引入了虚拟机的概念,即在机器和编译程序之间加入了一层抽象的虚拟的机器.这台虚拟的机器在任何平台上都提供给编译程序一个的共同的接口.编译程序只需要面向虚拟机,生成虚拟机能 ...
- Spring Cloud实战微服务入门
1.spring cloud是什么? 是一个快速构建分布式系统的工具集,构建于Spring Boot之上 2.spring cloud 的特点 约定优于配置 开箱即用.快速启动 适用于各种环境 轻量级 ...
- 智课雅思词汇---二十一、名词性后缀acity是什么意思
智课雅思词汇---二十一.名词性后缀acity是什么意思 一.总结 一句话总结:后缀:-acity [名词后缀] 构成抽象名词,表示性质.状态.情况.与形容词后缀-acious相对应 rapacity ...