看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717

思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值。

然后还要注意N>=K的时候,只能减1才能到达。

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <fstream>
#include <queue>
using namespace std;
struct node{
int pos,step;
}p,q;
int N,K,mmin;
bool visit[200010];
void bfs()
{
queue<node> Q;
memset(visit,0,sizeof(visit));
p.pos=N;
p.step=0;
Q.push(p);
visit[N]=1;
while(!Q.empty())
{
p=Q.front();
Q.pop();
if(p.pos==K){
if(p.step<mmin)mmin=p.step;
}
for(int i=0;i<3;i++)
{
if(i==0){ //x+1;
if(p.pos>K)
continue;
q.pos=p.pos+1;
if(visit[q.pos])continue;
q.step=p.step+1;
Q.push(q);
visit[q.pos]=1;
}
else if(i==1){ //x-1; q.pos=p.pos-1;
if(visit[q.pos])continue;
if(q.pos<0)continue;
q.step=p.step+1;
Q.push(q);
visit[q.pos]=1;
}
else if(i==2){ //2*x;
if(p.pos>K)continue;
q.pos=2*p.pos;
if(visit[q.pos])continue;
if(q.pos>100000)continue;
q.step=p.step+1;
Q.push(q);
visit[q.pos]=1;
} }
}
}
int main()
{
//ifstream fin;
//fin.open("data1.txt");
while(cin>>N>>K)
{
mmin=99999;
if(N>=K){
cout<<N-K<<endl;
}
else {
bfs();
cout<<mmin<<endl;
} } return 0; }

HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............的更多相关文章

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

  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)

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

  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)

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

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

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

  7. 杭电 HDU 2717 Catch That Cow

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

  8. HDU 2717 Catch That Cow (深搜)

    题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants t ...

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

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

随机推荐

  1. CxSkinButton按钮皮肤类

    在codeproject 发现一个很强大的按钮皮肤类,之前的版本有内存泄露,但是作者已经修复了,原文网址是:http://www.codeproject.com/KB/buttons/cxskinbu ...

  2. poj 3266 Cow School 分数规划

    这个题目难度非常大,首先对于老师的一种方案,应用分数规划的一般做法,求出所有的c=t-rate*p,如果没有选择的c值中的最大值比选择了的c值中的最小值大,那么这个解是可以改进的. 那么问题就转化成了 ...

  3. google浙大招聘笔试题 师兄只能帮你到这儿了

    google浙大招聘笔试题 一.单选1.80x86中,十进制数-3用16位二进制数表示为?00100002.假定符号-.*.$分别代表减法.乘法和指数运算,且 1)三个运算符优先级顺序是:-最高,*其 ...

  4. eclipse导入myeclipse的web项目没法识别问题解决方法

    1.进入项目目录,找到.project文件,打开. 2.找到<natures>...</natures>代码段. 3.在第2步的代码段中加入如下标签内容并保存: <nat ...

  5. english: 遭遇

    遭遇 [zāo yù] 1 (碰上: 遇到) meet with; encounter; run up against meet with misfortune; have hard luck 遭遇不 ...

  6. Mac 安装配置启动Tomcat

    Tomcat Mac 下的安装: TomCat 下载地址,例如: http://tomcat.apache.org/download-70.cgi 在Mac 上下载的时候,下载tar.gz包 下载完成 ...

  7. 基于visual Studio2013解决C语言竞赛题之1063分橘子

       题目 解决代码及点评 /* 功能:某桔农家共有2520只桔子, 父亲要将它们分给六个儿子,其分法如下: 父亲先将2520只桔子分给六个儿子, 然后说:"老大,把你分到的桔子 ...

  8. 1.1-学习Opencv与MFC混合编程之---利用画图函数,生成视频,并写入视频文件

    源代码地址:http://download.csdn.net/detail/nuptboyzhb/3961674 写视频文件 Ø 新建菜单项,Learning OpenCV——> OpenCVr ...

  9. .net面试题大全(有答案)

    在网上找来的,希望对大家有所帮助.     1 (1)面向对象的语言具有__继承性_性._封装性_性._多态性 性. (2)能用foreach遍历访问的对象需要实现 _ IEnumerable 接口或 ...

  10. shell oracle

    #!/bin/sh traffic= rm -rf test.txt data=`sqlplus -S anoscfg/anoscfg <<EOF spool test.txt set f ...