看题: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. 在JS中,一个自定义函数如何调用另一个自定义函数中的变量

    function aa1511() { var chengshi="马鞍山"; var shengfen="安徽省"; return shengfen+&quo ...

  2. linux下tomcat shutdown后 java进程依然存在

    今天遇到一个非常奇怪的问题,如标题所看到的: linux下(之所以强调linux下,是由于在windows下正常),运行tomcat ./shutdown.sh 后,尽管tomcat服务不能正常訪问了 ...

  3. EasyUI - Datatable转Json and Json转Datatable

    using System; using System.Data; using System.Linq; using System.Collections; using System.Collectio ...

  4. php 控制页面跳转

    <?php class UserAction extends Action{ public function index(){ echo "你好!"; $m=M('user' ...

  5. VS2008下编译BOOST 1.39的ASIO库

    由于全部编译BOOST库需要的时间太长,而且耗费空间,况且我只需要用ASIO库,所以就没有必要全部编译了. boost库到www.boost.org上下载. 编译很简单,假设你的boost存放的目录是 ...

  6. Central Europe Regional Contest 2012 Problem H: Darts

    http://acm.hunnu.edu.cn/online/problem_pdf/CERC2012/H.pdf HUNNU11377 题意:飞镖环有十个环,没个环从外到里对应一个得分1~10,每个 ...

  7. IT忍者神龟之中的一个句sql语句——连接同一字段的全部值

    Oracle能够用SYS_CONNECT_BY_PATH字符串聚合函数: SELECT LTRIM(MAX(SYS_CONNECT_BY_PATH(productname, ', ')), ', ') ...

  8. PowerDesigner反projectM连接ySql没有mySql odbc驱动器

    PowerDesignerfang反project连接MySql没有mySql odbc驱动器 需要安装 MySql ODBC驱动器.百度下载mysql-connector-odbc-5.3.4-wi ...

  9. Swift - 文本输入框(UITextField)的用法

    1,文本框的创建,有如下几个样式: UITextBorderStyle.None:无边框 UITextBorderStyle.Line:直线边框 UITextBorderStyle.RoundedRe ...

  10. ASP.NET - 分页

    效果: SQL-存储过程(Paging): ROW_NUMBER() over(order by MessageDateTime desc)  其中的 MessageDateTime desc 代表的 ...