bfs。

 /* 2717 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std; #define MAXN 100001 queue<int> Q;
int s[MAXN];
int n, k;
int ans; void bfs() {
int cur, nxt; while (!Q.empty())
Q.pop(); memset(s, , sizeof(s)); Q.push(n);
while (!Q.empty()) {
cur = Q.front();
Q.pop();
if (cur == k)
break;
nxt = cur - ;
if (nxt>= && s[nxt]==) {
Q.push(nxt);
s[nxt] = s[cur] + ;
}
nxt = cur + ;
if (nxt<MAXN && s[nxt]==) {
Q.push(nxt);
s[nxt] = s[cur] + ;
}
nxt = cur + cur;
if (nxt<MAXN && (nxt-k)<(k-cur) && s[nxt]==) {
Q.push(nxt);
s[nxt] = s[cur] + ;
}
}
} int main() { #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif while (scanf("%d %d", &n, &k) != EOF) {
if (n >= k)
printf("%d\n", n-k);
else {
bfs();
printf("%d\n", s[k]);
}
} return ;
}

【HDOJ】2717 Catch That Cow的更多相关文章

  1. HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............

    看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...

  2. 【HDOJ】2531 Catch him

    简单BFS.就是要把所有的D点当成一个整体考虑(整体移动). /* 2531 */ #include <iostream> #include <queue> #include ...

  3. 【搜索】C - Catch That Cow

    #include<stdio.h> #include<string.h> struct A{ int state; int step; }queue[]; // 结构体数组用来 ...

  4. 【POJ】3278 Catch That Cow

    题目链接:http://poj.org/problem?id=3278 题意:有一头奶牛跑到了K的位置,农夫在N的位置,求最短抓到奶牛的时间. 农夫有两种移动方式. 1.步行:一分钟内从x->x ...

  5. HDU 2717 Catch That Cow (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...

  6. 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,最先 ...

  7. HDU 2717 Catch That Cow(常规bfs)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...

  8. hdoj 2717 Catch That Cow【bfs】

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. HDU 2717 Catch That Cow(BFS)

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

随机推荐

  1. Java语言基础(二)

    Java语言基础(二) 一.变量续 (1).变量有明确的类型 (2).变量必须有声明,初始化以后才能使用 (3).变量有作用域,离开作用域后自动回收 变量作用域在块内有效 (4).在同一定义域中变量不 ...

  2. vim 缩写abbreviation

    创建 :ab abbreviation pharse 取消 :unab abbreviation 缩写使用 insert模式下输入缩写,Enter键获得pharse.

  3. 使用PHP实现蜘蛛访问日志统计

    $useragent = addslashes(strtolower($_SERVER['HTTP_USER_AGENT'])); if (strpos($useragent, 'googlebot' ...

  4. css3 calc()

    概述 CSS函数calc()可以用在任何一个需要<length>的地方.有了calc(),你可以通过计算来决定一个对象的大小和形状. 你还可以在一个calc()内部嵌套另一个calc(). ...

  5. myEclipse修改deploy location

  6. SQL语句之二建表

    use Test --进入需要建表的数据库if exists(select * from sysobjects where name='MyTable')--表是否已经存在drop table MyT ...

  7. php中调用其他系统http接口的方法说明

    使用函数: file_get_contents($url); 传入接口url及其参数:如 $url="http://192.168.1.1/test.jsp?id=1&type=2& ...

  8. Netbeans IDE配置

  9. Eclipse中看java源代码

    如何在Eclipse sdk中查看jar源代码如:*.jar 1.点 “window”-> "Preferences" -> "Java" -> ...

  10. WCF存储图片到指定文件夹下

    string path = System.IO.Directory.GetCurrentDirectory() + @"\POIImages\"; Guid imgid = Gui ...