题目链接: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. mnist的格式说明,以及在python3.x和python 2.x读取mnist数据集的不同

    有一个关于mnist的一个事例可以参考,我觉得写的很好:http://www.cnblogs.com/x1957/archive/2012/06/02/2531503.html #!/usr/bin/ ...

  2. Visual Studio 2017 Android 调试无法连接到虚拟机

    输出窗口输出如下: 1>Starting deploy 4.5" KitKat (4.4) HDPI Phone ... 1>Starting emulator 4.5" ...

  3. haproxy反向代理

    haproxy是个高性能的tcp和http的反向代理.它就是个代理.不像nginx还做web服务器 官网地址为www.haproxy.org nginx的优点和缺点 优点: 1.web服务器,应用比较 ...

  4. 剑指offer 6.查找和排序 旋转数组的最小数字

    题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋 ...

  5. [ZZ] Valse 2017 | 生成对抗网络(GAN)研究年度进展评述

    Valse 2017 | 生成对抗网络(GAN)研究年度进展评述 https://www.leiphone.com/news/201704/fcG0rTSZWqgI31eY.html?viewType ...

  6. 【转】搭建和配置Syncthing发现和中继服务器

    搭建和配置Syncthing发现和中继服务器     折腾借口 Syncthing好是挺好的,但就是同步不给力,公共网络服务其质量参差不齐,网络也说不清.最好的解决方案还是自己搭建发现服务器Synct ...

  7. C# .NET 实体类转Dictionary

    -- System.Reflection.PropertyInfo[] cfgItemProperties = cfgItem.GetType().GetProperties(System.Refle ...

  8. 判断文件是否存在,不要用if exist和if not exist,因为他们会受到文件是否隐藏的影响,改用dir /a 命令代替

    @echo off & setlocal enabledelayedexpansionrem 判断文件是否存在,不要用if exist和if not exist,因为他们会受到文件是否隐藏的影 ...

  9. 2016310Exp4 恶意代码及分析

    网络对抗 Exp4 恶意代码分析 实验内容 系统运行监控 恶意软件分析 报告评分 基础问题回答 实践目标 实验内容 1. 系统运行监控——计划任务 2. 系统运行监控——利用Sysmon 3.1恶意软 ...

  10. Google SketchUp Cookbook: (Chapter 5) Roofs: Constraints and Inferences

    软件环境 SketchUp Pro 2018 参考书籍 Google SketchUp Cookbook Creating an Overhanging Roof 屋顶上有一个缝隙需要填充,有两种方法 ...