M - 搜索

Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

  1. 5 17

Sample Output

  1. 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.
题目大意:农夫去追他跑丢的牛,题目给出了他和牛的位置,用数字N和K表示,假定牛不动,问农夫移动到牛的位置的最小步数,农夫每次的移动有三种选择:位置加1,位置减1,位置乘2.
思路分析:求最小步数,用BFS即可水过,不过由于本弱BFS刚刚入门,在做题的时候还是出现了很多问题,BFS一般要采用队列来进行实现,刚开始使用的是queue<int>队列,但是在对步数的保存上出现了问题,这时候我选择了queue<pair <int,int> >来记录位置和步数,每次将n+1,n-1,n*2都压入到队列当中去,直到n==m,结束。程序可以运行,样例的输出答案也是正确的,但是提交的时候MLE了,这让我明白了剪枝的重要性,最后程序在两个方面进行了剪枝,一个是建立一个标记数组,对搜索到的每一点都进行标记,避免重复搜索,第二就是对于n>m的情况,毫无疑问应该采取n-1。应用了这两点剪枝策略以后,成功A掉。
代码:#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
const int maxn=4e5+5;
int n,m;
queue<pair<int,int> >q;
bool hash[maxn];
int main()
{
    int m,n;
  while(cin>>n>>m)
  {
    memset(hash,false,sizeof(hash));
    pair<int,int> p;
    p.first=n;
    p.second=0;
    hash[n]=true;
    q.push(p);
    while(!q.empty())
    {
      pair<int,int> a,b;
      a=q.front();
      if(a.first==m)
      {
          cout<<a.second<<endl;
          break;
      }
     a.second++;
     b=a;
     if(b.first>m)
     {
         b.first=a.first-1;
         if(hash[b.first]==false)
         {
           hash[b.first]=true;//标记该点,表示已经走过
           q.push(b);
         }
     }
     if(b.first<m)
     {
         b.first=a.first+1;
         if(hash[b.first]==false)
         {
           hash[b.first]=true;//标记该点,表示已经走过
           q.push(b);
         }
         b.first=a.first*2;
        if(hash[b.first]==false)
         {
           hash[b.first]=true;
           q.push(b);
         }
         b.first=a.first-1;
         if(hash[b.first]==false)
         {
           hash[b.first]=true;
           q.push(b);
         }
     }
     q.pop();//弹出队列第一个元素
    }
    while(!q.empty())
        q.pop();//注意清空队列
  }
    return 0;
}
梦想起航!加油!

poj3278 BFS入门的更多相关文章

  1. HDU1548- A strange lift (BFS入门)

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A Strrange lift Time Limit: 2000/1000 MS (Java/ ...

  2. POJ-3278(BFS)

    题目:                                                                                                 ...

  3. POJ 3278 抓奶牛(BFS入门题)

    描述 农夫约翰已被告知逃亡牛的位置,并希望立即抓住她.他开始于一个点Ñ(0≤ Ñ ≤100,000)上的数线和牛是在点ķ(0≤ ķ上相同数目的线≤100,000).农夫约翰有两种交通方式:步行和传送. ...

  4. hdu 1242 Rescue(BFS入门)

    第一次用容器做的BFS题目,题目有个地方比较坑,就是遍历时的方向,比如上下左右能AC,右上左下就WA #include <stdio.h> #include <string.h> ...

  5. BFS入门

    #include<iostream> #include<cstring> #include<queue> using namespace std; #define ...

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

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

  7. HDU2717-Catch That Cow (BFS入门)

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/O ...

  8. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  9. poj3278 【BFS】

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 97240   Accepted: 30519 ...

随机推荐

  1. java学习之关键字

    java语言当中的关键字,之所以存在,是为了告诉编译器如何解释一段有意义的代码段.比如说 /**需求:演示java中关键字存在的含义步骤:用class,public,static,void等说明什么是 ...

  2. COJ 0970 WZJ的数据结构(负三十)树分治

    WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...

  3. CH Round #58 - OrzCC杯noip模拟赛day2

    A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...

  4. POJ-3189-Steady Cow Assignment(最大流+枚举)

    题意 此题题意不太好懂.现有n头牛和b个牛棚,每个牛棚可以养的牛的数目都有一个限制c[i],表示该牛棚最多只能关c[i]头牛,每头牛对每一个牛棚都有一个喜爱值,用1到b来表示,现在要安排这些牛,使得牛 ...

  5. Power Calculus 快速幂计算 (IDA*/打表)

    原题:1374 - Power Calculus 题意: 求最少用几次乘法或除法,可以从x得到x^n.(每次只能从已经得到的数字里选择两个进行操作) 举例: x^31可以通过最少6次操作得到(5次乘, ...

  6. hdu4666 最远曼哈顿距离

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4666 #include <cstdio> #include <cstring> ...

  7. poj2255

    题目大意: 树恢复??树复原?? 小Valentine非常喜欢玩二叉树的游戏,他非常喜欢在二叉树的树根上随机的写上一下大写字母,这是她创造的一个例子: D / \ / \ B E / \ \ / \ ...

  8. 348. Design Tic-Tac-Toe

    提示给的太直白了.. 比如player 1占据了(0,1),那么row[0]++ col[1]++ 表示第一行有1个O,第一列有1个X,假设PLAYER 1最终在第一行连成一排,那最终row[0] = ...

  9. spring 定时器----quartz启动问题

    今天,突然要用到定时器,在网上查了下资料,保存下以方便后面查找: 什么是动态定时任务:是由客户制定生成的,服务端只知道该去执行什么任务,但任务的定时是不确定的(是由客户制定). 这样总不能修改配置文件 ...

  10. [HDU 2049] 不容易系列之(4)——考新郎 (错排问题)

    不容易系列之(4)——考新郎 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2049 题目大意: 有N对新婚夫妇,其中所有的新娘站成一列,都盖上了红布. ...