题意:

从一个坐标到另一个坐标的移动方式有三种,即:st-1,st+1,2*st。每移动一步时间是一秒。

给出两个坐标,求得从第一坐标到第二座标的最短时间。

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=100005;
int step[maxn];
int st,ed;
void bfs(){
int key,t;
queue<int>que;
que.push(st);
while(!que.empty()){
key=que.front();
que.pop();
if(key==ed)
break;
t=key+1;
if(t<maxn&&step[t]==0){
step[t]=step[key]+1;
que.push(t);
}
t=key-1;
if(0<=t&&t<maxn&&step[t]==0){
step[t]=step[key]+1;
que.push(t);
}
t=key*2;
if(t<maxn&&step[t]==0){
step[t]=step[key]+1;
que.push(t);
}
}
}
int main ()
{
scanf("%d%d",&st,&ed);
memset(step,0,sizeof(step));
bfs();
printf("%d\n",step[ed]);
return 0;
}

1209:Catch That Cow(bfs)的更多相关文章

  1. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

  2. POJ3278——Catch That Cow(BFS)

    Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...

  3. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

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

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

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

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

  6. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  7. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  8. POJ3278 Catch That Cow —— BFS

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  9. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

随机推荐

  1. NEU OJ 1649 GMZ’s Pretty Number

    先来一次线性素数筛,把1到10000000的素数都筛选出来,然后暴力跑一遍所有可能的值,打个表,查询的时候o(1)效率出解. #include<cstdio> #include<cs ...

  2. nmon命令用法

    用途 以交互方式显示本地系统统计信息并以记录方式记录系统统计信息. 语法 交互方式: nmon [ -h ] nmon [ -s < seconds > ] [ -c < count ...

  3. C# var 隐式类型 var 用法 特点

    var 关键字是C# 3.0 开始,在方法范围中声明的变量: var有以下特点: * 1.var在编译器编译的时候根据初始值推断出其的类型          * 2.不能赋值除了初始值类型之外的其他类 ...

  4. 在Activity之间传递数据—简单数据/Bundle

    1.首先要知道怎么通过一个Activity 打开另一个Activity.主页面为MainActivity,另一个页面为OtherActivity.MainActivity中的按钮Id为btnStart ...

  5. css3-多列显示文字

    -------------------------- <style>            #demo{                margin: 20px auto;         ...

  6. Hunters

    Hunters Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submi ...

  7. git 提高下载速度

    1.  直接下载分支,就不用下载不需要的源码了. git clone --depth 1 git://github.com/TI-OpenLink/wl18xx.git  --branch ol_r8 ...

  8. vim 设置默认显示行号

    vim 要默认打开的时候显示行号,这样就可以不用每次编辑的时候在手动在命令模式下:set number 来显示行号. 具体做法是: 创建一个 .vimrc的配置文件. 在启动vim时,当前用户根目录下 ...

  9. 这几个linux 命令

    原文: linux性能分析 http://www.cnblogs.com/peida/tag/linux%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90/ du -sh /da ...

  10. git push origin master 报错 remote rejected] master -> master (branch is currently checked out)

    解决办法: 977down vote You can simply convert your remote repository to bare repository (there is no wor ...