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

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

题意:输入两个数n,k。求从n到k最少走多少步。能够前进1后退1或者当前的位置*2。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
struct node
{
int x;//当前位置
int ans;//走的步数
}q[1000010];
int vis[1000010];//标记变量,该点是否被訪问;
int jx[]={-1,1};//后退1或者前进1。
struct node t,f;
int n,k;
void bfs()
{
int i;
int s=0,e=0;//指针模拟队列。 往队列加e++ 往队列里提出数s++
memset(vis,0,sizeof(vis));
t.x=n;//当前初始位置
vis[t.x]=1;//标记为1代表訪问过。
t.ans=0;//初始位置步数为0;
q[e++]=t;//把当前步数加人队列
while(s<e)//当队列不为空
{
t=q[s++];//提出
if(t.x==k)//假设该数正好等于目标位置直接输出步数
{
printf("%d\n",t.ans);
break;
}
for(i=0;i<3;i++)//i=0后退一步,i=1前进一步。i=2此时的位置*2;
{
if(i==2)
{
f.x=t.x*2;
}
else
{
f.x=t.x+jx[i];
}
if(f.x>=0&&f.x<=100000&&!vis[f.x])
{
f.ans=t.ans+1;
q[e++]=f;
vis[f.x]=1;
}
}
}
}
int main()
{
while(~scanf("%d %d",&n,&k))
{
bfs();
}
return 0;
}

Catch That Cow(广度优先搜索_bfs)的更多相关文章

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

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

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

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

  3. poj-3278 catch that cow(搜索题)

    题目描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...

  4. POJ - 3278 Catch That Cow 简单搜索

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

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

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

  6. catch that cow POJ 3278 搜索

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

  7. Catch The Caw——(广度优先搜索的应用,队列)

    抓住那头牛(POJ3278)农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0<=K<=100000).农夫有 ...

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

  9. Poj 3287 Catch That Cow(BFS)

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

随机推荐

  1. MFC CString GetBuffer/ReleaseBuffer 的使用条件

      今天为了通过串口往单片机里写一个16进制字符去控制单片机的adc的起停,结果糊涂到把'\x01'误写成'\0x01',怎么也得不到意想的结果,程序员有时候会犯低级错误,有时候怎么也跳不出去,这时候 ...

  2. linux缺页异常处理--用户空间【转】

    转自:http://blog.csdn.net/vanbreaker/article/details/7870769 版权声明:本文为博主原创文章,未经博主允许不得转载. 用户空间的缺页异常可以分为两 ...

  3. c# GDI画图 双缓冲画图分析

    双缓冲绘图分析  1.Windows 绘图原理  我们在 Windows 环境下看到各种元素,如菜单.按钮.窗口.图像,从根本上说,都是“画”出来的.这时的屏幕,就相当于一块黑板,而 Windows ...

  4. SpringBoot部署到Linux服务器

    今天来谈一下,用SpringBoot写的Demo来配置到Linux服务器. 这里,我用阿里云服务器作为实例,SpringBoot集成了SpringMvC+ mybatis 框架,用maven管理jar ...

  5. (4) python--seaborn

    seaborn封装了matplotlib的一些风格,简单的介绍一下

  6. pythontips(2):hasattr的用法

    class Xiaorui: def __init__(self): self.name = 'xiaorui' def setName(self, name=''): if name.strip() ...

  7. (1)C# 创建ef sqlserver

    连接sql 如果报错不能连接的错误 把这三个IP地址的端口号设置上,并启用.第一个18.6是本机ip,之后就可以测试了 最后重启服务器

  8. Tiny6410下的第一个Linux驱动程序

    Linux系统环境是照着友善之臂的教程搭建的 //Hello  World驱动程序源文件 #include <linux/miscdevice.h> #include <linux/ ...

  9. 全面理解java自动装箱和拆箱(转)

    自动装箱和拆箱从Java 1.5开始引入,目的是将原始类型值转自动地转换成对应的对象.自动装箱与拆箱的机制可以让我们在Java的变量赋值或者是方法调用等情况下使用原始类型或者对象类型更加简单直接. 如 ...

  10. JS 随机数字抽签

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...