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. 【原创】Linux环境下的图形系统和AMD R600显卡编程(9)——R600显卡的3D引擎和图形流水线

    1. R600 3D引擎 R600核心是AMD一款非常重要的GPU核心,这个核心引入了统一处理器架构,其寄存器和指令集同以前的GPU 都完全不同,对其编程也有比较大的区别. 图1显示了R600 GPU ...

  2. entitymanager 进行数据序列化

    场景:同一个方法里,需要将前一部分执行的数据保存到数据库.后半部分读取数据时从数据库里获取,而不是获取到缓存里的数据. 理解eneityManager的这三个方法的作用和区别,首先需要分清楚Persi ...

  3. electron 安装使用

    1.安装 node.js 链接:http://pan.baidu.com/s/1o7W7BIy 密码:y6od 一路next 我安装在F:\Program Files\node.js下 2.检查nod ...

  4. HTML5 canvas上画文字出现乱码

    不是这个的问题, <html> <head> <meta charset="utf-8"/> </head> 是因为从网上复制粘贴下 ...

  5. hdu 5063(思路题-反向操作数组)

    Operation the Sequence Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. @Html.Raw()用法

    @Html.Raw() 方法输出带有html标签的字符串, 如:@Html.Raw("<div style='color:red'>输出字符串</div>" ...

  7. HDU 2063.过山车-Hungary(匈牙利算法)

    过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. (32)C#文件读写

    一.File 类 这是一个静态类,提供用于创建.复制.删除.移动和打开单一文件的静态方法,并协助创建 FileStream 对象 using System.IO; 没有构造函数和属性  写入数据 1. ...

  9. ESXI和vSphere的安装配置-实现一台电脑硬件虚拟化为两台

    本篇文章包含以下几个部分: 1EXSI软件和vSphere软件的安装 2在vSphere上安装虚拟系统 3对虚拟系统通过配置实现硬件虚拟化,实现硬件直通 1.EXSI安装 通过网上下载EXSI ISO ...

  10. 用jmeter进行多用户并发压力测试

    测试要求如下,多用户同时登陆web应用程序,并进行操作,查看在多用户操作下,程序的performence.恰好,jemter下有个CSV Data Set Config,它用来设定一组参数,以便在向程 ...