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. nodejs通过代理(proxy)发送http请求(request)

    有可能有这样的需求,需要node作为web服务器通过另外一台http/https代理服务器发http或者https请求,废话不多说直接上代码大家都懂的: var http = require('htt ...

  2. C#让应用程序只运行一个实例的几种方法

    一 判断是否有相同的实例已经运行 1 根据“Mutex”判断是否有相同的实例在运行 /// <returns>已有实例运行返回true,否则为false</returns>pu ...

  3. java打印随机函数

    一 ,打印1-10的随机函数 public static void randomprint(){      for (int i=0;i<100;i++){          //打印一百次  ...

  4. 牛客国庆集训派对Day7 Solution

    A    Relic Discovery 水. #include <bits/stdc++.h> using namespace std; int t, n; int main() { s ...

  5. HDU5183 hash 表

    做题的时候忘了 数据结构老师说的hash表了, 用二分找,还好过了, hash 表 对这题 更快一些 #include <iostream> #include <algorithm& ...

  6. uva10417 概率DP

    这题 to[i][j] 为第i个人送j这个礼物的概率 我们用13进制进行压缩这个留下的的礼物的个数,这样我们将dp[i][k]表示为当第i个人放完礼物后得到的状态为k时的概率,那么通过记忆化搜索我们就 ...

  7. 892. Surface Area of 3D Shapes

    问题 NxN个格子中,用1x1x1的立方体堆叠,grid[i][j]表示坐标格上堆叠的立方体个数,求这个3D多边形的表面积. Input: [[1,2],[3,4]] Output: 34 思路 只要 ...

  8. Python numpy 安装以及处理报错 is not a supported wheel on this platform

    1.    安装 1)去这里搜索https://pypi.org/ 2)搜索框输入numpy 3)一般第一个就是搜索到的 4)点进去 5) Download files 点进去,找自己的版本 6)nu ...

  9. laravel + html ajax 多表单字段和图片一起上传

    $("#article_push").on('click', function (e){ e.preventDefault(); var stylestr = $('#summer ...

  10. Object-C开发之instancetype和id关键字

    一.什么是instancetypeinstancetype是clang 3.5开始,clang提供的一个关键字,表示某个方法返回的未知类型的Objective-C对象.我们都知道未知类型的的对象可以用 ...