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. Android入门 在ListView中如何进行精确的定位

      在android的开发中,经常会遇到需要主动去设定某条ListItem的位置的需求.设置位置的函数有 ListView.setSelection(int position) ListView.se ...

  2. PHP性能:序——谈性能

    PHP性能:序——谈性能 这里不谈PHP的性能和其他语言的性能,这里讨论PHP自身的性能问题. 性能是什么? 通俗的来讲,性能,就是在固定的环境下能做的事情的多少. 为什么要性能? 1.每一个软件或网 ...

  3. GDB + gdbserver 远程调试mediaserver进程

    远程调试步骤 在Android设备上启动gdbserver并attach你想调试的进程,并指定监听调试命令的端口(此端口是TV上的端口) $ adb shell # ps |grep media  # ...

  4. Python标识符

    在python里,标识符有字母.数字.下划线组成. 在python中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. python中的标识符是区分大小写的. 以下划线开头的标识符是有 ...

  5. RHEL7安装部署ZooKeeper

    转载请注明出处:jiq•钦's technical Blog - 季义钦 文章说明: 分布式注冊中心(链接)须要安装的组件包括两个部分: 1.注冊中心服务(Zookeeper) 2.站点(Tomcat ...

  6. 在Linux中显示日历(cal)

    cal 2013    显示2013年整年日历 cal 7 2013  显示2013年 7 月 日历

  7. 【BZOJ2322】[BeiJing2011]梦想封印 高斯消元求线性基+DFS+set

    [BZOJ2322][BeiJing2011]梦想封印 Description 渐渐地,Magic Land上的人们对那座岛屿上的各种现象有了深入的了解. 为了分析一种奇特的称为梦想封印(Fantas ...

  8. Black And White(DFS+剪枝)

    Black And White Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others ...

  9. 【HTML5开发系列】表单元素

    <form> 创建一个HTML表单 属性: action 表示提交表单时浏览器应该把用户填写的数据发送到什么地方 method 用来指定表单数据发送到服务器的方式.允许值有get和post ...

  10. 九度OJ 1198:a+b (大数运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6745 解决:2320 题目描述: 实现一个加法器,使其能够输出a+b的值. 输入: 输入包括两个数a和b,其中a和b的位数不超过1000位 ...