[poj] 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
Output
Sample Input
5 17
Sample Output
4
Hint
三个搜索方向 +1, -1, *2 用bfs搜索 数组要开到最大值的2倍 注意剪枝和边界问题
#include <iostream>
#include <stdio.h>
#include <queue>
#include <cstring>
using namespace std; const int Max = ;
int s, e;
struct node
{
int x, step;
}now, Next;
bool v[];
queue<node>q; int bfs()
{
now.x = s;
now.step = ;
v[s] = ;
q.push(now);
while (!q.empty()) {
now = q.front();
q.pop();
if (now.x == e)
return now.step; Next.x = now.x*;
Next.step = now.step+;
if (Next.x >= && Next.x <= Max && !v[Next.x]){
q.push(Next);
v[Next.x] = ;
} Next.x = now.x-;
Next.step = now.step+;
if (Next.x >= && Next.x <= Max && !v[Next.x]){
q.push(Next); //若先检查v[Next.x] 会出现数组越界v[-1]的情况
v[Next.x] = ;
} Next.x = now.x+;
Next.step = now.step+;
if (Next.x >= && Next.x <= Max && !v[Next.x]){
q.push(Next);
v[Next.x] = ;
} }
return ;
} int main()
{
//freopen("1.txt", "r", stdin);
cin >> s >> e;
memset(v, , sizeof(v));
cout << bfs(); return ;
}
[poj] Catch That Cow--bfs的更多相关文章
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行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 ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- POJ3278——Catch That Cow(BFS)
Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
- 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 ...
随机推荐
- PHP生成图片太慢了。。有些都不出来、
现在为了使用不同宽高的图片,做了个动态生成的程序.每次根据图片传入的宽高来输出图片,然后 html 页面里用 <img src="xxx.com/img?src=c8d997dae15 ...
- 常用js方法函数
常用方法函数 1.深复制 // 1.深复制 function deepCopy(source) { var result = {}; for (var key in source) { result[ ...
- css 样式(checkbox开关、css按钮)
checkbox开关 css .iosCheck { /* Blue edition */ } .iosCheck input { display: none; } .iosCheck i { dis ...
- Swift - 修改导航栏的样式(文字颜色,背景颜色,背景图片)
默认情况,导航栏UINavigationController的样式如下,如果想要使用代码修改样式也是比较简单的. 1,修改导航栏背景色 1 2 3 //修改导航栏背景色 self.navigation ...
- LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式 的解决方法
一.案例1,及解决方案: "LINQ to Entities 不识别方法"System.String ToString()",因此该方法无法转换为存储表达式." ...
- shell将字符串转换为大写变量并将小写作为变量值
group_name='a b c d e f g' for a in $group_name; do typeset -u a ; echo "$a='$(echo $a | tr '[A ...
- JavaScript(3)
var a=90; switch(a){ case "890": window.alert("ok"); break; case 90: window.aler ...
- html的head中的常见元素
<head></head>中有charset, title,link 操作系统默认的字符编码就是gbk. html的加强 (1)<a href="#" ...
- FAQrobot 聊天机器人笔记
follow: https://github.com/ofooo/FAQrobot 这是一个简单的基于问词匹配的自动问答,获取与用户问句Q1最匹配的知识库中的问句Q2,Q2的答案就是Q1的答案. 首 ...
- APIO2018爆零记
Day1 集合 7点和yyc他们在学校简单的集合了一下 在大通道看到了整个年级来上操 嘲讽了一番就大摇大摆的走出了校门 校门口看无迟到周的权益部长lzj同学满眼的羡慕 2333 然后到了裕龙酒店登记入 ...