个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时;

将每个动作扔入队列,但要注意如何更简便,更节省时间,空间

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?

InputLine 1: Two space-separated integers: N and KOutputLine 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.
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int sum;
int ok=;
struct Node
{
int x;
int y; };
int book[]; void dfs(int n,int m)
{
memset(book,,sizeof(book));
queue<Node >s;
book[n]=;
Node t;
t.x=n;t.y=;
s.push(t);
int a;
Node tt;
while(!s.empty())
{
a=s.front().x*;
tt.x=a,tt.y=s.front().y+;
if(a==m)
{
sum=tt.y;
return ; }
if(tt.x>=&&tt.x<=)
if(!book[a])
{
book[a]=;
s.push(tt); }
a=s.front().x+;
tt.x=a;
tt.y=s.front().y+;
if(a==m)
{
sum=tt.y;
return ; }
if(tt.x>=&&tt.x<=)
if(!book[a])
{
book[a]=;
s.push(tt); };
a=s.front().x-;
tt.x=a,tt.y=s.front().y+;
if(a==m)
{
sum=tt.y;
return ; }
if(tt.x>=&&tt.x<=)
if(!book[a])
{
book[a]=;
s.push(tt); }
s.pop(); }
return ; }
int main()
{ int n,m;
while(cin>>n>>m)
{
sum=;
if(n>=m) sum=n-m;
else
dfs(n,m);
cout<<sum<<endl; }
return ; }

Catch That Cow(广搜)的更多相关文章

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

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

  2. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  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. Catch That Cow(BFS广搜)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  5. poj 3278 Catch That Cow (广搜,简单)

    题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...

  6. HDU2717 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)

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

  8. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  9. 广搜 poj3278 poj1426 poj3126

    Catch That Cow Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Ja ...

随机推荐

  1. try catch 事务不会滚

    在spring机制中,在配置事务后,如果采用try catch 捕获异常后,因为异常已经被捕获,所以事务不会滚,从而产生许多脏数据.解决办法: 1.在catch中抛出异常,(throw new Run ...

  2. linux常用技巧(资料)

    Linux中查看程序安装位置 如果是rpm的安装,用rpm -ql如果是一般安装 用 whereis 或者 find find /usr -name catalina.out======== 如何查看 ...

  3. springboot-vue项目后台1

  4. CSS3手风琴菜单 可同时折叠多个菜单

    在线演示 本地下载

  5. processing学习整理---Input(输入)

    1.鼠标1D. 左右移动鼠标可移动天平. “mouseX”变量用于控制矩形的大小和颜色. void setup(){ size(640,360); noStroke(); colorMode(RGB, ...

  6. 回溯法之k着色问题

    package main import ( "fmt" ) type Graphic struct { edges [][]int colors int color []int f ...

  7. mysql与mongodb命令对比

    连接:mysql: mysql -h localhost -u username -pmongodb:con = pymongo.Connection(‘localhost’,27017)显示数据库m ...

  8. 应用程序.f/q(f了个墙)

    1.20180414: 前两天,发现 CnFast不能用了,也没管它 以为过两天会好. 今天发现还是不能用,上网随便baidu下,看到个帖子:有人用加速精灵吗 现在登陆不了了 哪位大佬知道怎么回事啊[ ...

  9. json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

    转:json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException 执行:JSONArray arr ...

  10. 万字总结:学习MySQL优化原理,这一篇就够 了!【转】

    说起MySQL的查询优化,相信大家收藏了一堆奇技淫巧:不能使用SELECT *.不使用NULL字段.合理创建索引.为字段选择合适的数据类型..... 你是否真的理解这些优化技巧?是否理解其背后的工作原 ...