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 ...
随机推荐
- 20145231 《Java程序设计》第一次实验
实验一 Java开发环境的熟悉(Windows+IDEA) 实验内容 使用JDK编译.运行简单的Java程序: 使用IDEA编辑.编译.运行.调试java程序: 实验知识点 JVM.JRE.JDK的安 ...
- debug(实验)
一.用到的简单的DOS命令: cd\ ——首先要用cd\ 退回到根目录C>下 dir ——显示文件列表 md hb ——建立hb子目录 cd hb ——进入hb子目录 copy d:\dos\m ...
- java基础之final/static/static final
一.final 1.final修饰变量(常量) final修饰的成员变量表示常量,一旦给定初值既无法改变 2.final方法 final修饰方法,表示该方法不能被子类重写 好处:比非final方法要快 ...
- 关于Hystrix
RPC远程调用过程中如何防止服务雪崩效用 微服务中如何保护服务 Hystrix是一个微服务中关于服务保护框架,在分布式中能够实现对服务容错.出错之后的预备方案 背景 在今天,基于SOA的架构已经大行其 ...
- nohup后台运行jar与关闭
nohup 用途:LINUX命令用法,不挂断地运行命令. 语法:nohup Command [ Arg ... ] [ & ] 描述:nohup 命令运行由 Command 参数和任何相关 ...
- Dom4j quick start guide
Parsing XML Using Iterators Powerful Navigation with XPath Fast Looping Creating a new XML document ...
- R语言可视化
R语言基础(一) 可视化基础 ##数据获取 x1=round(runif(100,min=80,max=100)) x2=round(rnorm(100,mean=80, sd=7)) x3=ro ...
- 配置JSP模板
- http://www.cnblogs.com/jscode/archive/2012/09/03/2669299.html
http://www.cnblogs.com/jscode/archive/2012/09/03/2669299.html
- PHP error_get_last() 函数
error_get_last() 函数返回最后发生的错误 返回了一个关联数组,描述了最后错误的信息,以该错误的 "type". "message"." ...