http://poj.org/problem?id=3278

从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内,

注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=2e5+3;
int n,k;
int dp[maxn];
queue<int>que;
int main(){
scanf("%d%d",&n,&k);
memset(dp,0x7f,sizeof(dp));
dp[n]=0;
que.push(n);
bool fl=false;
while(!que.empty()){
int f=que.front();que.pop();
if(f==k){printf("%d\n",dp[f]);break;}
if(f-1>=0&&dp[f-1]>dp[f]+1){
que.push(f-1);
dp[f-1]=dp[f]+1;
}
if(f+1<=2*k&&dp[f+1]>dp[f]+1){
que.push(f+1);
dp[f+1]=dp[f]+1;
}
if(f<k&&dp[2*f]>dp[f]+1){
que.push(2*f);
dp[2*f]=dp[f]+1;
}
}
return 0;
}

  

POJ 3278 Catch That Cow bfs 难度:1的更多相关文章

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

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

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

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

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

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

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

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

  5. 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 ...

  6. poj 3278 Catch That Cow bfs

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  7. poj 3278 Catch That Cow(bfs+队列)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  8. POJ - 3278 Catch That Cow bfs 线性

    #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...

  9. BFS POJ 3278 Catch That Cow

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

随机推荐

  1. MegaCli 监控raid状态 限戴尔服务器

    MegaCli 监控raid状态 MegaCli是一款管理维护硬件RAID软件,可以通过它来了解当前raid卡的所有信息,包括 raid卡的型号,raid的阵列类型,raid 上各磁盘状态,等等.通常 ...

  2. 13.Github使用

    我们一直用GitHub作为免费的远程仓库,如果是个人的开源项目,放到GitHub上是完全没有问题的.其实GitHub还是一个开源协作社区,通过GitHub,既可以让别人参与你的开源项目,也可以参与别人 ...

  3. (2.12)Mysql之SQL基础——存储过程条件定义与错误处理

    转自:博客园桦仔 5.存储过程条件定义与错误处理 -- (1)定义 [1]条件定义:declare condition_name condition for condition_value;[2]错误 ...

  4. js-jquery-从SweetAlert到SweetAlert2

    原文地址:https://github.com/limonte/sweetalert2/wiki/Migration-from-SweetAlert-to-SweetAlert2 1. IE supp ...

  5. selenium自我手册

    (转载需注明原文地址和作者两项,否则视为非授权) 语言:python 0x00 预热 下载安装包 pip install selenium 确定所用的浏览器 支持firefox,chrome,IE,e ...

  6. js判断浏览器的类型和获得浏览器的版本

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...

  7. cocos代码研究(23)Widget子类ScrollView学习笔记

    基础理论 一个能够被用户触摸滚动的一个层次型布局容器视图,允许其尺寸大于屏幕显示的物理尺寸,其内部维护有一个布局用于水平的或垂直的存放子节点.继承自 Layout,被 ListView 继承. 代码实 ...

  8. SQL Server与Oracle对表添加列的不同点

    逛了博客园两年有余,不知道该发表些什么.要么觉得自己太菜,要么觉得要发的内容都可以搜索到,发表了还颇有抄袭味道.想想后都不得了之了. 搞了开发快一年了,有时候零零碎碎的东西需要整理一下,梳理后才能做到 ...

  9. Horizon代码的层次结构

    Horizon中包含多个dashboard,每个dashboard又包含多个panel,每个panel有可以包含多个Tab.

  10. ansible之template模块

    趁着最近在搞ansible,现在学习了一波template模块的用法: 1.使用template模块在jinja2中引用变量,先来目录结构树 [root@master ansible]# tree . ...