POJ3278 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
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 220000
int n,k;
int vis[maxn]; struct Node
{
int x, time; }node[maxn]; void bfs()
{
queue<Node> Q;
Node r,t,f;
r.time = ; r.x = n;
vis[n] = ;
Q.push(r);
while(!Q.empty())
{
f = Q.front();
Q.pop();
if(f.x == k)
{
printf("%d\n", f.time);
return;
}
if(f.x < k&&!vis[f.x*])
{
t.x = f.x*;
vis[t.x] = ;
t.time = f.time+;
Q.push(t);
}
if(f.x+<=k && !vis[f.x+])
{
t.x = f.x+;
vis[t.x] = ;
t.time = f.time+;
Q.push(t);
}
if(f.x->= && !vis[f.x-])
{
t.x = f.x-;
vis[t.x] = ;
t.time = f.time+;
Q.push(t);
}
}
}
int main()
{
while(~scanf("%d%d", &n, &k))
{
if(k <= n)
{
printf("%d\n", n-k);
continue;
}
memset(vis, , sizeof vis);
bfs();
}
return ;
}
POJ3278 Catch That Cow(BFS)的更多相关文章
- POJ3278——Catch That Cow(BFS)
Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- 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,最先 ...
- poj3278 Catch That Cow(简单的一维bfs)
http://poj.org/problem?id=3278 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 73973 Accepted: 23308 ...
- 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,板子题)
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: 61826 Accepted: 19329 ...
随机推荐
- 股票市场问题(The Stock Market Problem)
Question: Let us suppose we have an array whose ith element gives the price of a share on the day i. ...
- 算法导论(第三版)习题Exercises4.3(第四章三节)算法导论的一个印刷错误
本节系列证明都可见4.5节需要说明的有4.3-8,4.3-9两题 4.3-8(本题有误) T(n)=4T(n/2)+n2根据4.5理论,结果为Θ(n2lgn) 4.3-9 m = lgn T(2m) ...
- WPF - Build Error总结
1. are you missing an assembly reference 给项目添加新控件的时候,经常发现这种错误 Error 21 The type or namespace name 'C ...
- js高级程序设计(第三版)学习笔记(第一版)
ecma:欧洲计算机制造商协会iso/iec:国际标准化和国际电工委员会 dom级别(10*)文档对象模型1:DOM核心(映射基于xml文档)与dom html(在dom核心基础上)2:对鼠标,事件, ...
- Android 按二次后退键退出应用程序
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- 由mysql数据库基础上的php程序实现单词的查询、删除、更改和查询
我做了一个php程序,将表单数据添加到数据库,借用mysql扩展库函数实现对mysql数据库的操作,能够实现添加单词.删除单词.更新和查询单词.运行环境是普通的mysql数据库和php.Apache服 ...
- 使用VTemplate模板引擎动态生成订单流程图
1.VTemplate模板引擎的简介 VTemplate模板引擎也简称为VT,是基于.NET的模板引擎,它允许任何人使用简单的类似HTML语法的模板语言来引用.NET里定义的对象.当VTemplate ...
- C# 大小写转换
全部大写: string upper = str.ToUpper() 全部小写: string lower = str.ToLower(); str是需要转换的字符.
- js数组 函数
js数组 filter(),map(),some(),every(),forEach(),lastIndexOf(),indexOf() 文章1:http://www.jb51.net/article ...
- IEquatable(T) interface in .Net
原文:http://weblogs.asp.net/pawanmishra/iequatable-t-interface-in-net 泛型方法: public static bool AreEqua ...