HDU 2717

  题目大意:在x坐标上,农夫在n,牛在k。农夫每次可以移动到n-1, n+1, n*2的点。求最少到达k的步数。

  思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先找到的一定是最小的步数。

/* HDU 2717 Catch That Cow --- BFS */
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; bool visit[];
int n, k; struct Node{
int num;
int step;
Node(int lhs = , int rhs = ) :num(lhs), step(rhs){}
}; inline bool judge(int x){
if (x < || x > || visit[x])
return ;
return ;
} void bfs(){
memset(visit, , sizeof visit);
queue<Node> q;
visit[n] = ; //标记起点已访问
q.push(n); while (!q.empty()){
Node x = q.front(); q.pop();
if (x.num == k){
printf("%d\n", x.step);
break;
}
Node y = x;
++y.step; //第一个方向 x-1
y.num = x.num - ;
if (judge(y.num)){
visit[y.num] = ; //标记该点已访问
q.push(y);
} //第二个方向 x+1
y.num = x.num + ;
if (judge(y.num)){
visit[y.num] = ; //标记该点已访问
q.push(y);
} //第三个方向 2*x
y.num = x.num * ;
if (judge(y.num)){
visit[y.num] = ; //标记该点已访问
q.push(y);
}
}//while(q)
} int main()
{
while (scanf("%d%d", &n, &k) == ){
bfs();
}
return ;
}

HDU 2717 Catch That Cow --- BFS的更多相关文章

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

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

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

  4. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

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

  5. hdu 2717 Catch That Cow(广搜bfs)

    题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...

  6. 杭电 HDU 2717 Catch That Cow

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

  7. 题解报告:hdu 2717 Catch That Cow(bfs)

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

  8. hdu 2717 Catch That Cow(BFS,剪枝)

    题目 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> ...

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

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

随机推荐

  1. Section 1.4 Mother's Milk

    又是一道怨念已久的题目0 0之前深搜写过广搜写过,怎么就是卡死,我还以为FP坏了重新装了一遍.今天偶尔翻起来,发现广搜忘记inc(head)了…简直哭瞎… 简单的广搜,分类比较多,不过不太要动脑子.至 ...

  2. 北大ACM题库习题分类与简介(转载)

    在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------- ...

  3. web api post传一个参数时 值永远是null

    这个问题纠结了我一个早上,不管用什么样的传参方法,走到控制器中,那个参数永远不变的等于null 在网上找了很多解决方案 上面这个是从网上截图的,第一:要将参数标记为[FromBody],变为简单参数 ...

  4. unity3d角色控制器01

    参考出处貌似是雨松大神.如有侵权,立即删除. 需要导入包 ①将FirstPerson Controller拖拽入Hierarchy(层次视图)中.由于角色控制器是具有一定物理引擎的,所以一定要将它放在 ...

  5. php源码编译常见错误解决方案

    在CentOS编译PHP5的时候有时会遇到以下的一些错误信息,基本上都可以通过yum安装相应的库来解决.以下是具体的一些解决办法: checking for BZip2 support… yes ch ...

  6. jsp弹出Please check the location and try again!对话框

    关闭它的jsp图形模式.myeclipse10中打开jsp文件时,右键open with 选MyEclipse JSP Editor,不选MyEclipse Visual JSP Editor模式.

  7. svnUbuntu下安装与使用(ZT)

    1.安装包 1.$ sudo apt-get install subversion 2.创建项目目录 $ sudo mkdir /home/xiaozhe/svn $ cd /home/xiaozhe ...

  8. Fish入门

    安装以及配置 Linux和OS X基本都可以通过源来安装,实在不行就下载源码编译,不难的. 安装好第一步是修改OS默认shell: chsh -s /usr/bin/fish 然后就可以直接使用了,就 ...

  9. IOS 封装类的时候注释格式,使用的时候可以想官方库一样快捷显示

    /** @brief 详情 @param 参数 @note 注意 @return 返回值类型 @code 这里写例题代码 @endcode @see 相似的方法参考 */

  10. 使用java理解程序逻辑,变量

    数据类型 说明 举例 int 整型 用于存储整数.例如.学员人数.某公司的员工编号.一年的天数.一天的小时数 double 双精度浮点型 用于存储带有小数的数字.例如商品的价格.世界的价格.世界银行给 ...