Catch That Cow

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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. 

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<stack>
using namespace std;
#define N 100010
int time[N], maps[N], s, e;//将这些宏定义是为了避免传参;
void BFS();
int main()
{
while(scanf("%d%d", &s, &e)!=EOF)
{
memset(time, 0, sizeof(time));//定义的时间数组,初始化为零;本身定义了结构体,其中包含了位置location、时间time、标记变量flag。却发现flag无法初始化
memset(maps, 0, sizeof(maps));//标记是否走过
if(s==e)
printf("0\n");
else
BFS();
}
return 0;
}
void BFS()
{
queue<int>que;
int i, x, dir;
maps[s]=1;
que.push(s);
while(!que.empty())
{
x=que.front();
que.pop();
for(i=0; i<3; i++)
{
switch(i)//用switch更简便;
{
case 0: dir=x-1; break;
case 1: dir=x+1; break;
case 2: dir=x*2; break;
}
if(dir==e)//结束条件。其实也可放在for循环外,只不过是多进行了几步而已
{
printf("%d\n", time[x]+1);
return;
}
if(maps[dir]==0&&dir<N&&dir>=0)//这也算是剪枝,减少了重复
{
que.push(dir);
maps[dir]=1;
time[dir]=time[x]+1;
}
}
}
}

poj 3278 Catch That Cow-搜索进阶-暑假集训的更多相关文章

  1. poj 1426 Find The Multiple 搜索进阶-暑假集训

    E - Find The Multiple Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  2. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

  3. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

  4. POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

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

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

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

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

  7. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  8. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

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

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

随机推荐

  1. ZAP介绍

    Zed Attack Proxy简写为ZAP,是一个简单易用的渗透测试工具,是发现Web应用中的漏洞的利器,更是渗透测试爱好者的好东西.ZAP下载地址:https://www.owasp.org/in ...

  2. vim 命令行使用技巧

    1. <Ctrl-U> <Ctrl-K> 删除光标到开头的输入 2. <Ctrl-W> 删除最近输入的单词 3. <Ctrl-H> 删除光标之前的一个字 ...

  3. brew Error: Formulae found in multiple taps

    Mac PHP brew install php56-apcu Error: Formulae found in multiple taps: * homebrew/php/php56-apcu * ...

  4. 小技巧之Selenium如何切换到弹出的Tab页中

    今天群里讨论了一个问题,如何将selenium的操作焦点切换到浏览器中新弹出来的Tab页中,正好对应到了昨天的那篇文章“小技巧之在浏览器中打开新的页签”.今天就带大家来解决这个问题: 先封装一个Tab ...

  5. [ERROR] Error generating R.java from manifest

    把*.js文件用记事本打开,再保存为utf-8的编码覆盖,把build文件夹的文件删掉,启动.可以运行了.(在这之前试过把jdk的几个文件考到平台工具下的动作)

  6. 创建自己的Spring Boot Starter

    抽取通用模块作为项目的一个spring boot starter.可参照mybatis的写法. IDEA创建Empty Project并添加如下2个module,一个基本maven模块,另一个引入sp ...

  7. 《HBase in Action》 第二章节的学习总结 ---- HBase基本组成

    准备工作:采用的HBase版本是:CDH4.5,其中的Hadoop版本是:hadoop-2.0.0-cdh4.5.0:HBase版本是:hbase-0.94.6-cdh4.5.0: Hbase的配置文 ...

  8. spring-test-dbunit的配置和使用

    1.数据源配置 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" ...

  9. eclipse配置jp.gr.java_conf.ussiy.app.propedit_5.3.3

    配置PropertiesEditor插件 jp.gr.java_conf.ussiy.app.propedit_5.3.3   1.下载PropertiesEditor插件 http://pan.ba ...

  10. 什么是 Service Mesh

    作者|敖小剑 微服务方兴未艾如火如荼之际,在 spring cloud 等经典框架之外,Service Mesh 技术正在悄然兴起.到底什么是 Service Mesh,它的出现能带来什么,又能改变什 ...