题目链接:http://poj.org/problem?id=3278

ac代码:

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
int a,b;
int vis[100005];
queue<int> q;
int bfs(){
  if(a>=b) return a-b;
  int x;
  q.push(a);
  vis[a] = 0;
  while(!q.empty()){
  int t = q.front();q.pop();
  if(t==b) return vis[t];

  for(int i = 0;i<3;i++){
    if(i==0) x = t + 1;
    else if(i==1) x = t - 1;
    else x = t*2;
    if(x<0||x>100005) continue;
    if(vis[x]==-1){
      vis[x] = vis[t] + 1;
      q.push(x);
    }

  }
  }
}

int main(){
  while(scanf("%d%d",&a,&b)!=EOF){
    while(!q.empty()) q.pop();
    memset(vis,-1,sizeof(vis));
    printf("%d\n",bfs());
    }

}

POJ - 3278的更多相关文章

  1. 【BFS】POJ 3278

    POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最 ...

  2. BFS POJ 3278 Catch That Cow

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

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

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

  4. catch that cow POJ 3278 搜索

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

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

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

  6. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...

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

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

  8. POJ 3278 Catch That Cow(模板——BFS)

    题目链接:http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a f ...

  9. POJ 3278 Catch That Cow bfs 难度:1

    http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...

随机推荐

  1. js···元素的属性

    Div.attributes 是所有标签属性构成的数据集合 Div.classList 是所有class名构成的数组集合 在classList的原型链上看以看到add()和remove(). clie ...

  2. IntelliJ IDEA 实用快捷键

    psvm--------------主(main)-----------------public staitc void main(String[] args) sout--------------- ...

  3. centos7使用cronolog分割tomcat8.5的catalina.out日志

    1.安装cronolog wget https://files.cnblogs.com/files/crazyzero/cronolog-1.6.2.tar.gz tar -zxvf cronolog ...

  4. qt+opencv 构建项目时报错——no such file or directory

    构建前,记得,一定一定一定要先点击执行qmake:

  5. PymongoDB_study

    import pymongo client = pymongo.MongoClient(host='localhost',port=27017)#连接数据库 #db = client.test#指定数 ...

  6. 全志A33开发板的安卓控制LED-2-JNI基础

    虽然您可以完全使用Java编写应用程序,但有些情况下Java本身并不能满足您的应用程序的需求.当应用程序不能完全用Java编写时,程序员使用JNI编写Java本机方法来处理这些情况. 以下示例说明何时 ...

  7. KiCad 一款强大的 BOM 和 装配图生成插件

    KiCad 一款强大的 BOM 和 装配图生成插件 可以生成 BOM 和在线的图形. https://github.com/openscopeproject/InteractiveHtmlBom In ...

  8. C# CRC - 16

    using System; static class Program { static void Main() { string input = "8000"; var bytes ...

  9. lua 5.3最简单plugin编写

    #include <windows.h> #include "lauxlib.h" /* Pop-up a Windows message box with your ...

  10. TypeScript 学习资料

    TypeScript 学习资料: 学习资料 网址 TypeScript Handbook(中文版)(推荐) https://m.runoob.com/manual/gitbook/TypeScript ...