Catch That Cow

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 48   Accepted Submission(s) : 16
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
 
Source
PKU
 
 
这道题是到BFS水题,不过我在这道题上re了很多次。。。原因是我没有剪枝。
 
 
 
#include<iostream>
#include<cstring>
using namespace std;
int que[1000001]; //数组要开大谢;大数组用全局变量开,不能再函数里开。
int step[1000001]={0};
bool f[1000001]={false};
int n,k; int BFS()
{
int front=0,rear=1;
que[0]=n;
step[0]=0;
f[n]=true;
if(que[front]==k)
return step[front];
while(front<rear)
{
if(que[front]<k&&!f[que[front]+1]) //只有当前的数比k小时,加一才能更接近k。
{
que[rear]=que[front]+1;
step[rear]=step[front]+1;
f[que[rear]]=true;
if(que[rear]==k)
return step[rear];
rear++;
}
if(que[front]-1>=0&&!f[que[front]-1]) //数组不能越界
{
que[rear]=que[front]-1;
step[rear]=step[front]+1;
f[que[rear]]=true;
if(que[rear]==k)
return step[rear];
rear++;
}
if(que[front]<k&&!f[que[front]*2]) //只有当前的数比k小时,乘2才有可能更接近k。
{
que[rear]=que[front]*2;
step[rear]=step[front]+1;
f[que[rear]]=true;
if(que[rear]==k)
return step[rear];
rear++;
}
front++;
}
} int main()
{
while(cin>>n>>k)
{
memset(que,0,sizeof(que));
memset(step,0,sizeof(step));
memset(f,false,sizeof(f));
cout<<BFS()<<endl;
}
}

HDOJ-三部曲一(搜索、数学)-1006- Catch That Cow的更多相关文章

  1. 【搜索】C - Catch That Cow

    #include<stdio.h> #include<string.h> struct A{ int state; int step; }queue[]; // 结构体数组用来 ...

  2. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

  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 POJ 3278 搜索

    catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...

  5. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

  6. hdoj 2717 Catch That Cow【bfs】

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

  7. Catch That Cow(广度优先搜索_bfs)

     Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 48036   Accepted: 150 ...

  8. 2016HUAS暑假集训训练题 B - Catch That Cow

    B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...

  9. 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 ...

  10. Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 58072   Accepted: 18061 ...

随机推荐

  1. Java 基础知识点(必知必会其二)

    1.如何将数字输出为每三位逗号分隔的格式,例如“1,234,467”? package com.Gxjun.problem; import java.text.DecimalFormat; impor ...

  2. Symmetric Tree [LeetCode]

    Problem description: http://oj.leetcode.com/problems/symmetric-tree/ Basic idea: Both recursive and ...

  3. 报错解决:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    大概分析一般使用了注解才会报这方面的错 1.没有在spring的ApplicationContext.xml中开启注解事务 <!-- 开启注解事务 --> <tx:annotatio ...

  4. 《转》---使用递归方法DataTable 绑定 TreeView

    转自:http://blog.sina.com.cn/s/blog_8944756d01016yaj.html 前台: <asp:View ID="view0" runat= ...

  5. 《Play for Java》学习笔记(六)文件上传file upload

    一. Play中标准方法 使用表单form和multipart/form-data的content-type类型. 1.Form @form(action = routes.Application.u ...

  6. html5 <input> placeholder 属性

    带有 placeholder 文本的搜索字段: <form action="demo_form.asp" method="get"> <inp ...

  7. Ubuntu 设置su密码

    如果之前安装时没有设置root密码,可以如下设置: 命令窗口中输入:sudo passwd [sudo] password for 用户名:  这里输入你sudo 的密码输入新的 UNIX 密码: 重 ...

  8. Eclipse导出可执行Jar文件(包含第三方Jar包)

    1. 首先,右键你的Java工程,选择Export,在Java文件夹下选择Runnable JAR file,如下图所示: 2. 选择Runnable JAR file后,会弹出如下所示的对话框,选择 ...

  9. qml的打包问题

    qml2的打包问题: 相对于早期的项目,只需要打包plugin和动态库.带有sqlite的程序如果需要打包,需要打包如下东西: 1.打包AppData目录下的  Local/Qt Project/项目 ...

  10. web api control注册及重写DefaultHttpControllerSelector、ApiControllerActionSelector、ApiControllerActionInvoker

    namespace EWorkpal.WebApi { public class HttpNotFoundDefaultHttpControllerSelector : DefaultHttpCont ...