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. VMware vCenter Server安装图解教程

    安装说明: 1.安装VMware vCenter Server的主机操作系统为:Windows Server 2008 R2 2.在Windows Server 2008 R2中需要预先安装好SQL ...

  2. BestCoder Round #61 1002 Game

    Problem Description XY is playing a game:there are N pillar in a row,which numbered from 1 to n.Each ...

  3. String和StringBuffer的转换

    从String到StringBuffer: StringBuffer sb = New StringBuffer("abcd");从StringBuffer到String: Str ...

  4. C# 毕业证书打印《四》

    数据存储,读取控件在Panel中的位置,将控件的位置保存到xml文件中. /// <summary> /// 将当前格式写入xml /// </summary> /// < ...

  5. linux下打包文件或文件夹

    转自:    在linux下如何将文件夹打包                 http://blog.csdn.net/cynhafa/article/details/7303338 linux zi ...

  6. display:inline 遇上 li 无效? why?

    若制作导航栏时,使用列表li 的定义时,若想加上一个背景图 ,这时候若定义li的一个属性为:li{display:inline ; width:83px; height:30px;},则浏览器会无视后 ...

  7. Shell 语法 if 、 case 、for 、 while、 until 、select 、repeat、子函数

    if语法 :   if [ expression ]    then   commandselif [ expression2 ]   then   commandselse   commandsfi ...

  8. 用普通用户通过sudo进行启动tomcat时报如下异常

    用普通用户通过sudo进行启动tomcat时报如下异常 tomcat user 不在 sudoers 文件中.此事将被报告. 这是由于sudo命令使用root用户执行命令.而处于安全性的考虑,一般不允 ...

  9. ios Tabbar Item 的图标

    1,tabBarItem图片的推荐尺寸和最大支持尺寸 下面是标签栏(UITabBar)中tab按钮图标分别在1x.2x.3x下不会压缩变形的尺寸: @1x : 推荐 25 x 25   (最大: 48 ...

  10. ACM/ICPC 之 BFS-简单障碍迷宫问题(POJ2935)

    题目确实简单,思路很容易出来,难点在于障碍的记录,是BFS迷宫问题中很经典的题目了. POJ2935-Basic Wall Maze 题意:6*6棋盘,有三堵墙,求从给定初始点到给定终点的最短路,输出 ...