Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 44613   Accepted: 13946

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 - 1 or + 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

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

题意:有一个农民和一头牛,他们在一个数轴上,牛在k位置保持不动,农户開始时在n位置。设农户当前在M位置,每次移动时有三种选择:1.移动到M-1。2.移动到M+1位置;3.移动到M*2的位置。问最少移动多少次能够移动到牛所在的位置。所以能够用BFS来搜索这三个状态,直到搜索到牛所在的位置。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; const int N = 200100;
int n, k;
struct node
{
int x, step;
};
queue<node> q;
int vist[N]; void bfs()
{
int cow, ans;
while(!q.empty())
{
node tmp = q.front();
q.pop();
cow = tmp.x;
ans = tmp.step;
if(cow == k)
{
printf("%d\n",ans);
return ;
}
if(cow >= 1 && !vist[cow - 1]) //要保证减1后有意义,所以要cow >= 1 减一的情况
{
node temp;
vist[cow - 1] = 1;
temp.x = cow - 1;
temp.step = ans + 1;
q.push(temp);
}
if(cow <= k && !vist[cow + 1]) //加1的情况
{
node temp;
vist[cow + 1] = 1;
temp.x = cow + 1;
temp.step = ans + 1;
q.push(temp);
}
if(cow <= k && !vist[cow * 2]) //乘二的情况
{
node temp;
vist[cow * 2] = 1;
temp.x = 2 * cow;
temp.step = ans + 1;
q.push(temp);
}
}
} int main()
{
while(~scanf("%d%d",&n,&k))
{
while(!q.empty()) q.pop();
memset(vist,0,sizeof(vist));
vist[n] = 1;
node t;
t.x = n, t.step = 0;
q.push(t);
bfs();
}
return 0;
}

POJ 3278: Catch That Cow的更多相关文章

  1. POJ 3278:Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submi ...

  2. POJ 3278:The merchant(LCA&DP)

    The merchant Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6864   Accepted: 2375 Desc ...

  3. [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)

    BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...

  4. 抓住那只牛!Catch That Cow POJ-3278 BFS

    题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...

  5. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

  6. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  7. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

  8. POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  9. catch that cow POJ 3278 搜索

    catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...

随机推荐

  1. Codeforces Gym100952 A.Who is the winner? (2015 HIAST Collegiate Programming Contest)

      A. Who is the winner?   time limit per test 1 second memory limit per test 64 megabytes input stan ...

  2. 一个排好序的数组,找出两数之和为x的所有组合【双指针】

    #include <bits/stdc++.h> using namespace std; const int N = 1e6,INF = 0x3f3f3f3f; int a[N]; in ...

  3. hadoop学习二:hadoop基本架构与shell操作

    1.hadoop1.0与hadoop2.0的区别:

  4. mysql主从复制、读写分离

    一.MySql介绍 MySQL作为世界上使用最为广泛的数据库之一,免费是其原因之一.但不可忽略的是它本身的功能的确很强大.随着技术的发展,在实际的生产环境中,由单台MySQL数据库服务器不能满足实际的 ...

  5. [Contest20180313]灵大会议

    为了方便才用lct,没想到最后要加读入优化才能过... 有一个结论就是在一条链上,如果能找到一个点使得这个点划分链左右两边的树节点权值和最相近,那么这个点就是答案 用lct维护,每个splay节点存树 ...

  6. Scala实战高手****第2课:Scala零基础实战入门的第一堂课及如何成为Scala高手

    val声明的不可变的战略意义:1.函数式编程中要求值不可变,val天然符合这一特性:2.在分布式系统中,一般都要求值不可变,这样才能够要求分布式系统的设计和实现,同时拥有更高的效率,val声明的内容都 ...

  7. Android介绍

    Android系统的底层建立在Linux系统之上,该平台有操作系统,中间件,用户界面和应用软件4层组成,它采用一种被称为软件叠层(Software Stack)的方式进行构建. 1.应用程序层:And ...

  8. 连接sqlexpress

    sqlexpress在visualstudio安装时可选择安装. 数据源添加 localhost\sqlexpress window身份认证即可.

  9. Debian、Ubuntu 源列表说明

    转载:http://forum.ubuntu.org.cn/viewtopic.php?t=366506 概貌: 源列表主文件为 /etc/apt/sources.list,另兼取 /etc/apt/ ...

  10. Merge的山寨版“联机帮助”

    IF NOT OBJECT_ID('Demo_AllProducts') IS NULL DROP TABLE Demo_AllProducts GO CREATE TABLE Demo_AllPro ...