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. PHP和.NET通用的加密解密函数类,均使用3DES加解密 .

    以下为php代码 <PRE class=PHP name="code"> </PRE><PRE class=PHP name="code&q ...

  2. how to reset mac root password

    Reset 10.5 Leopard & 10.6 Snow Leopard password Power on or restart your Mac. At the chime (or g ...

  3. VS2013项目配置directx11

    假定你已经安装好了direct11 SDK(官方下的既是最新版本)和VS2013,并在VS2013中建立了一个新工程. 用VS2013打开工程后,点击右键属性,选择VC++目录: 其中在包含目录中添加 ...

  4. 黑马程序员——OC语言Foundation框架 结构体

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)结构体 NSRange(location length) NSPoi ...

  5. Vm下 linux与windowsxp文件共享的方法

    我的PC Operating System Host是Windows XP,Guest是Linux,virtualPC是VMware workstation.方法介绍大全请参见:Windows与Vmw ...

  6. C# MP3文件属性读取

    using (TempFile tempFile = new TempFile()) { using (FileStream fs = new FileStream(tempFile.FileName ...

  7. 统计一段文字中出现频率最高的10个单词(c语言)

    注:这次使用C语言做的这个程序.个别不懂的地方和算法部分是请教的其他同学,交流并吸收,所以收获颇多! 在程序中每一个地方我都做了注释,方便同学之间交流.也让老师容易看.程序也有很多不足的地方,但限于本 ...

  8. hadoop2.x通过Zookeeper来实现namenode的HA方案以及ResourceManager单点故障的解决方案

    我们知道hadoop1.x之前的namenode存在两个主要的问题:1.namenode内存瓶颈的问题,2.namenode的单点故障的问题.针对这两个问题,hadoop2.x都对它进行改进和解决.其 ...

  9. Cookie 与Session 的区别(转载)

    原地址: http://www.cnblogs.com/shiyangxt/archive/2008/10/07/1305506.html 两个都可以用来存私密的东西,同样也都有有效期的说法. 区别在 ...

  10. 利用pl/sql developer进行远程连接oracle server出现的问题及解决办法

    由于本人刚刚给自己的笔记本做了系统,由原来的32位系统编程现在的64位系统,所以,很多软件由于兼容性,不得不重新安装...当我安完了pl/sql developer工具后,就满心欢喜的去连接远程ora ...