传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717

Catch That Cow

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20259    Accepted Submission(s): 5926

Problem Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

 
Input
Line 1: Two space-separated integers: N and K
 
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
 
Sample Input
5 17
 
Sample Output
4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

 
Source
 
Recommend
teddy
 
 
题目意思:
一维线性地图
人的位置在n,牛的位置在k
人每走一步有三种选择:+1,-1,*2
问你最少的步数是多少?
 
分析:
很普通的bfs,只有三种操作,如果三种操作后的位置都合法的话,就入队
 
需要注意的地方:
判断是否越界的时候,不能像题目中所说,
当牧场主所在的位置大于10W的时候,就认为他越界。
  因为他有可能先去到
100010的时候 ,在回来。所以再判断的时候,
因为就算了一开始站在10w的位置,你最多跳2倍,也最多到20w
所以
越界的最大值最好为20W。这样就不会出错了。
 
code:
#include<stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <cstdlib>
#include <queue>
using namespace std;
#define max_v 100000
int vis[*max_v+];
int n,k;
struct node
{
int x,step;
};
int f(int x)//检查合法性
{
if(x<||x>=*max_v||vis[x]==)//超出范围或者用过
{
return ;
}
return ;
}
int bfs()
{
queue<node> q;
node p,next; p.x=n;
p.step=;
vis[n]=;
q.push(p); while(!q.empty())
{
p=q.front();
q.pop(); if(p.x==k)
{
return p.step;
} //+1,-1,*2 三种情况都加入队列
next.x=p.x+;
if(f(next.x))
{
next.step=p.step+;
vis[next.x]=;
q.push(next);
} next.x=p.x-;
if(f(next.x))
{
next.step=p.step+;
vis[next.x]=;
q.push(next);
} next.x=p.x*;
if(f(next.x))
{
next.step=p.step+;
vis[next.x]=;
q.push(next);
}
}
return -;
}
int main()
{
int ans;
while(cin>>n>>k)
{
memset(vis,,sizeof(vis));
ans=bfs();
cout<<ans<<endl;
}
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)

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

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

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

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

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

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

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

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

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

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

  8. hdoj 2717 Catch That Cow【bfs】

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

  9. 杭电 HDU 2717 Catch That Cow

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

随机推荐

  1. Linux终端和win32控制台文本颜色输出

    在使用putty.secureCRT.XShell等终端仿真器连接linux系统时,ls.vim等工具的输出都含有各种颜色,这些颜色的输出大大地增强了文本的可读性. 通常我们可以使用echo命令加-e ...

  2. vcfc之zk+postsql+keystore(cassandra)框架分析

    vcfc框架总结: 1 一. bus和keystore是如何协调处理的,什么样的问题是处理不了的? 1. 如果在备重启的过程中,主处理了某个时间1,备机如果同步数据呢? 二 .数据可靠性和一致性分析 ...

  3. svn的使用总结

    在网上看到了以前介绍非常全的svn的文章,拿来分享 原文网址  http://www.cnblogs.com/jx270/archive/2013/03/04/2943595.html 还有一篇更基础 ...

  4. 原生JS编写getByClass、addClass、removeClass、hasClass

    前言: 年后换了工作,在现在的公司写交互主要使用JS原生:刚刚入门前端的时候写交互一直用的原生JS,虽然用的不怎么样.后来去之前的公司之后,leader主张把jQuery用好,JS原生自然就熟练了:一 ...

  5. mysql load data infile auto increment id

    1. 问题描述 当使用load data infile 向表中插入数据 而主键id是 auto_increment 时 ,执行 load data 不会报错 但插入也不成功 2. 问题解决 2.1 方 ...

  6. javascript刷新页面的集中办法

    1. history.go(0) 2. location.reload() 3. location=location 4. location.assign(location) 5. document. ...

  7. 139.00.007 Git学习-Cheat Sheet

    @(139 - Environment Settings | 环境配置) Git虽然极其强大,命令繁多,但常用的就那么十来个,掌握好这十几个常用命令,你已经可以得心应手地使用Git了. 友情附赠国外网 ...

  8. 沉淀,再出发:Maven的使用和规范

    沉淀,再出发:Maven的使用和规范 一.前言 Maven作为项目管理工具,在一个大型项目开发的每个阶段都有着很大的用处,为什么需要这个东西呢,还是为了消除不确定性,统一化管理,正如我们做的每一件事其 ...

  9. 工作好搭档(三):慧想 S100 液晶显示器支架

            引言:工欲善其事,必先利其器.码农十年,与电脑打了二十多年的交道,也配置了一些过得去的装备.资金有限,更希望所有的投入都在刀刃上.写工作好搭档系列,是晒考虑的原因.思路.经验和教训.欢 ...

  10. OKEX期现对冲JS源代码分享(基于Fmz, Botvs实现)

    什么是期现对冲?此策略风险和收益如何?期现对冲是利用期货和现货之间存在的差价进行套利.因为在交割日的时候,期货会按现货价格成交,当期货和现货一旦出现差价时,就可以通过做空期货做多现货(或做多期货卖出现 ...