HDU 2717 Catch That Cow --- BFS
题目大意:在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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2717 Catch That Cow(广搜bfs)
题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...
- 杭电 HDU 2717 Catch That Cow
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 题解报告:hdu 2717 Catch That Cow(bfs)
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
- hdu 2717 Catch That Cow(BFS,剪枝)
题目 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> ...
- HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............
看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...
随机推荐
- 怎么用navicat自动备份mysql数据库
打开navicat客户端,连上mysql后,双击左边你想要备份的数据库.点击“计划”,再点击“新建批处理作业”. 双击上面的可用任务,它就会到下面的列表里去,代表你选择了这个任务. 点击保存, ...
- Java基础01 ------ 从HelloWorld到面向对象
Java是完全面向对象的语言.Java通过虚拟机的运行机制,实现“跨平台”的理念.我在这里想要呈现一个适合初学者的教程,希望对大家有用. "Hello World!" 先来看一个H ...
- 破解Xamarin
试用了一阵子Mono For Android,今天到期了,,囊中羞涩,只好破解. 说是要在vs2013的英文界面下运行破解包,不知道是真是假,下载并安装了一个. 然后又下载了破解包.是个名为xa.ra ...
- android开发艺术探索
android开发艺术探索 百度任玉刚 http://blog.csdn.net/singwhatiwanna/article/details/46810527
- hdu1506 dp
//Accepted 1428 KB 62 ms // #include <cstdio> #include <cstring> #include <iostream&g ...
- Ubuntu 14.10 下安装中文输入法
系统默认带的是IBUS,这个不怎么好用,我们需要安装一个新的框架FCITX 1 打开软件中心,输入fcitx,安装flexible input method framework 2 下载需要的输入法, ...
- hdoj-2023
#include "stdio.h"int main(){ int a[55][6]; double pingjun[55],mk[6]; int n,m,i,j,sum=0,co ...
- IIS6的session丢失问题
解决办法: a IIS6中相比IIS5增加了一个应用程序池,默认是使用DefaultAppPool. b 先为站点建立一个应用程序池,打开IIS管理器,右键点击应用程序池-新建 ...
- 深入学习:Windows下Git入门教程(下)
声明:由于本人对于Git的学习还处于摸索阶段,对有些概念的理解或许只是我断章取义,有曲解误导的地方还请见谅指正! 一.分支 1.1分支的概念. 对于的分支的理解,我们可以用模块化这个词来解释:在日常工 ...
- CSS样式选择器
<!-- css样式选择器 HTML选择器 类选择器 ID选择器 关联选择器 组合选择器 伪元素选择器 selector{ /* selector是样式选择器 property:value; / ...