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 ...
随机推荐
- python的对象类型-----列表&元组&字典
列表: #定义列表 l=[1,'a',[3,4]] #l=list([1,'a',[3,4]]) #取值 print(l[0]) print(l[2][0]) l=[1,2,[['a','b'],'c ...
- debian内核代码执行流程(二)
继续上一篇文章<debian内核代码执行流程(一)>未完成部分. acpi_bus_init调用acpi_initialize_objects,经过一系列复杂调用后输出下面信息: [ IN ...
- 图片的另一种展现—将后台图片编码直接展现为图片
1.应用场景 开发过程中,遇到这样的需求:需要将服务器上的图片展现在页面上,但是图片所在服务器不是对外的,图片所在服务器与应用服务器也不在同一台机器上,这时候就需要在开发中先将图 ...
- iostream与iostream.h的区别
简单来说: .h的是标准C的头文件,没有.h的是标准C++的头文件,两种都是头文件. 造成这两种形式不同的原因,是C++的发展历史决定的,刚才正好有别的人也问这个问题,这里我再回答一下(注意vs200 ...
- 16个tomcat面试题
1)解释什么是Jasper? Jasper是Tomcat的JSP引擎 它解析JSP文件,将它们编译成JAVA代码作为servlet 在运行时,Jasper允许自动检测JSP文件的更改并重新编译它们 2 ...
- margin和text-align以及align
margin和text-align是css样式,align是html的 <style> .test { width: 400px; height: 200px; background: g ...
- 【P2629】好消息,坏消息(前缀和+单调队列优化DP)
一激动一颓就会忘了总结... 前面的大黄题就不总结了. 这个题我只想说一声艹,一开始的思路就是正确的,然后计算的时候有了一个瑕疵,不过很快也就改过来了.然后却一直连样例都过不了.仔仔细细看了老半天,经 ...
- Mybatis单个参数的if判断(针对异常:There is no getter for property..)------mybatis的内置对象
这里有一个删除方法: int deleteByPrimaryKey(Integer id); 然后对应的sql的xml如下: <delete id="deleteByPrimaryKe ...
- Mac键盘图标与对应快捷按键标志汇总 分类
Mac键盘图标与对应快捷按键 ⌘——Command () win键 ⌃ ——Control ctrl键 ⌥——Option (alt) ⇧——Shift ⇪——Caps Lock fn——功能键就是 ...
- Liberty glance 新功能 healthcheck
oslo.middleware‘s healthcheck http://specs.openstack.org/openstack/oslo-specs/specs/kilo/oslo-middle ...