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. WCF - 序列化

    数据是信息的载体 在不同环境中有不同的类型 为保证处于不同平台的的应用能够正常的进行数据交互 必须采用一种双方都能理解的数据类型 XML无疑是最好的选择 但不是唯一的选择 例如JSON也是一种普遍认可 ...

  2. tcmalloc资料

    1. 确定dylib在max os是可以成功的. http://lists.apple.com/archives/perfoptimization-dev/2008/Dec/msg00002.html ...

  3. 设置ViewController 数据源无法改变view

    病情描述: viewController创建的时候勾选了xib,然后在显示的时候调用了如下语句: MTDetailDealViewController *detailController = [[MT ...

  4. Sql Server 中事务(begin tran/commit tran/rollback tran)的用法

    ALTER PROCEDURE [dbo].[Proc_Test_commit1]     @result int output, --成功 1; 失败 0     @message nvarchar ...

  5. android入门系列- TextView EditText Button ImageView 的简单应用

    第一篇原创,其实自己就是一菜鸟,简单分享点基本知识吧.希望能有所帮助吧. TextView EditText Button ImageView 这几个控件可能是Android开发中最常用.最基本的几个 ...

  6. 在往oracle中插数据时,如何处理excel读取的时间空值

    //若从excel中读取的时间值为空值时,做如下转换 string YDKGSJ = string.Empty; if (dbdata.Rows[i]["约定开工时间"].ToSt ...

  7. 由App的启动说起(转)

    The two most important days in your life are the day you are born and the day you find out why. -- M ...

  8. inno setup 多语言安装

    之前的安装程序默认语言为英文,现在我们需要将它变成中文,由于InnoSetup安装包中默认没有带中文语言文件,我们需要下载一个先: 到http://www.400gb.com/u/758954/123 ...

  9. Composite 模式的实现

    实现要点: 1.组合模式采用树形结构来实现普遍存在的对象容器,从而将“一对多”的关系转化“一对一”的关系,使得客户代码可以一致地处理对象和对象容器,无需关心处理的是单个的对象,还是组合的对象容器. 2 ...

  10. SGU 122.The book (哈密顿回路)

    题目描述 有一群人从1到N标号,而且这群人中每个人的朋友个数不少于 (N+1)/2 个. 编号为1的人有一本其他人都想阅读的书. 写一个程序,找到一种传阅顺序使得书本只经过每个人手中一次,并且一个人只 ...