Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 76935   Accepted: 24323

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

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

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
 
题意:给出两个数star,end,给出x-1,x+1,x*2四种运算,使star变成end;
 
思路:直接bfs就好了;
 
代码:
 #include <iostream>
#include <string.h>
#include <queue>
#define MAXN 200000+10
using namespace std; struct Node //***用结构体表示节点,x表示当前值,step记录当前步数
{
int x, step;
}; int star, end, vis[MAXN]; int bfs(int x)
{
Node a, b, c;
a.x = x, a.step = ; //***赋初值
queue<Node>q; //***用队列实现
q.push(a); //***头节点入队
while(!q.empty()) //***如果队列不为空,继续循环
{
b = q.front(); //***取当前队列的头节点做父节点并将其从队列中删除
q.pop();
vis[b.x] = ; //***标记
if(b.x == end) //***如果达到目标,返回步数
{
return b.step;
}
c = b; //***符合条件的儿子入队
if(c.x >= && !vis[c.x-])
{
c.x-=;
c.step++;
vis[c.x]=;
q.push(c);
}
c = b;
if(c.x < end && !vis[c.x+])
{
c.x+=;
c.step++;
vis[c.x]=;
q.push(c);
}
c = b;
if(c.x < end && !vis[*c.x])
{
c.x*=;
c.step++;
vis[c.x]=;
q.push(c);
}
}
return -;
} int main(void)
{
while(cin >> star >> end)
{
if(star >= end) //***如果star>=end,则每次减1,最少需要star-end步
{
cout << star - end << endl;
continue;
}
memset(vis, , sizeof(vis)); //***标记数组清0,不然会对后面的测试产生影响
int ans=bfs(star); //***深搜
cout << ans << endl;
}
return ;
}

http://poj.org/problem?id=3278(bfs)的更多相关文章

  1. POJ3278http://poj.org/problem?id=3278

    http://poj.org/problem?id=3278 题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步 #include<stdio.h> #include&l ...

  2. poj 1651 http://poj.org/problem?id=1651

      http://poj.org/problem?id=1651Multiplication Puzzle   Time Limit: 1000MS   Memory Limit: 65536K To ...

  3. poj-3056 http://poj.org/problem?id=3056

    http://poj.org/problem?id=3056 The Bavarian Beer Party Time Limit: 6000MS   Memory Limit: 65536K Tot ...

  4. poj 1679 http://poj.org/problem?id=1679

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  5. poj 1915 http://poj.org/problem?id=1915

    /**< */#include <stdio.h> #include <string.h> #include <stdlib.h> #include < ...

  6. 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258

    #include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...

  7. Roadblocks http://poj.org/problem?id=3255

    Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best ...

  8. http://poj.org/problem?id=2253

    floyd的应用求每条路径两点之间最大距离的最小值 #include <iostream> #include <cstdio> #include <algorithm&g ...

  9. 线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468

    题目链接 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...

随机推荐

  1. 剑指Offer 从尾到头打印链表

    题目描述 输入一个链表,从尾到头打印链表每个节点的值. 输入描述: 输入为链表的表头 输出描述: 输出为需要打印的“新链表”的表头 思路: 用容器vector,递归到最后一个元素,push_back到 ...

  2. Win7去除桌面残影的方法

    用户升级到Win7系统后使用正常,就是系统桌面会留有残影,怎么样也去不掉,影响用户的使用,那么要如何将这些残影去掉呢?可从计算机属性中进行相关配置. 解决方法 一.在计算机面板上,右键点击“计算机”, ...

  3. Python自动化之socket初识

    1. os.popen() os.system(cmd)会直接输出命令的结果到屏幕上,返回一个状态码0或1. os.popen(cmd)会返回一个<open file 'dir', mode ' ...

  4. TextMate 小小心得

    在Vim.Emacs之间纠结了很久之后,却选择了TextMate P.S. 为何Emacs和Vim被称为两大神器 中文的资料不是很多,一狠心,找了James Edward Gray II的TextMa ...

  5. LIGHTSWITCH 连接 MYSQL,中文字符不能保存----解决方法。

    使用:dotConnect for MySQL () 作为 数据库连接的PROVIDER ,  在 LIGHTSWITCH 中 引用外部的MYSQL 数据源. http://www.devart.co ...

  6. POJ 2479

    ---恢复内容开始--- http://poj.org/problem?id=2479 #include <stdio.h> #include <iostream> using ...

  7. poj 3984

    http://poj.org/problem?id=3984 题目很简单,就是简单的BFS吧,主要的难点在于坐标的问题 这个呢,可以反其道而行之,就是你从(1,1)到(5,5),你肯定走过一次 走过一 ...

  8. pip安装简单方法

    前提:有网络 wget -c --no-check-certificate https://bootstrap.pypa.io/get-pip.py python get-pip.py

  9. 调试WebService

    1.运行WebService的调用程序 2.浏览器中运行asmx,这一步是为了让w3wp.exe出现在下一步的列表中 3.“工具”或“调试”菜单-->附加到进程 (MS为什么把同一功能放在不同的 ...

  10. 数据结构——二叉查找树、AVL树

    二叉查找树:由于二叉查找树建树的过程即为插入的过程,所以其中序遍历一定为升序排列! 插入:直接插入,插入后一定为根节点 查找:直接查找 删除:叶子节点直接删除,有一个孩子的节点删除后将孩子节点接入到父 ...