题目链接:
http://poj.org/problem?id=3278

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.
题意描述:
输入人和牛在坐标轴上的位置
按照人寻找的三种走路方式,问最短抓到牛的时间。
解题思路:
搜索题,求最短时间,使用BFS更合适一点。另外需要注意:
   走过的点是不需要重复走的,因为既然走过就证明牛不在这个点,所以使用book数组标记一下就不会出现超内存(扩展无用点)和超时啦。
AC代码:
 #include<stdio.h>

 int bfs(int n,int k);
struct node
{
int x,s;
};
struct node q[];
int book[];//标记数组,否则超内存 int main()
{
int n,k;
while(scanf("%d%d",&n,&k) != EOF)
{
if(n==k)
printf("0\n");
else
printf("%d\n",bfs(n,k));
}
return ;
}
int bfs(int n,int k)
{
int i,head,tail,tx;
head=;
tail=;
q[tail].x=n;
q[tail].s=;
book[n]=;
tail++; while(head < tail)
{
for(i=;i<=;i++)
{
if(i==)
tx=q[head].x-;
if(i==)
tx=q[head].x+;
if(i==)
tx=q[head].x*; if(tx < || tx > )
continue;
if(!book[tx])
{
book[tx]=;
q[tail].x=tx;
q[tail].s=q[head].s+;
tail++; if(tx == k)
return q[tail-].s;
}
}
head++;
}
}

POJ 3278 Catch That Cow(模板——BFS)的更多相关文章

  1. POJ 3278 Catch That Cow(bfs)

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

  2. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...

  3. POJ 3278 Catch That Cow(简单BFS)

    题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次 ...

  4. POJ - 3278 Catch That Cow 【BFS】

    题目链接 http://poj.org/problem?id=3278 题意 给出两个数字 N K 每次 都可以用三个操作 + 1 - 1 * 2 求 最少的操作次数 使得 N 变成 K 思路 BFS ...

  5. POJ 3278 Catch That Cow(BFS 剪枝)

    题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出 ...

  6. POJ 3278 Catch That Cow【BFS】

    题意:给出n,k,其中n可以加1,可以减1,可以乘以2,问至少通过多少次变化使其变成k 可以先画出样例的部分状态空间树 可以知道搜索到的深度即为所需要的最小的变化次数 下面是学习的代码----@_@ ...

  7. POJ——3278 Catch That Cow(BFS队列)

    相比于POJ2251的三维BFS,这道题做法思路完全相同且过程更加简单,也不需要用结构体,check只要判断vis和左右边界的越界情况就OK. 记得清空队列,其他没什么好说的. #include< ...

  8. BFS POJ 3278 Catch That Cow

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

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

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

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

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

随机推荐

  1. DNS查询的工作原理

    二.DNS查询的工作原理 1.DNS查询过程按两部分进行     1.名称查询从客户端计算机开始, 并传送给本机的DNS客户服务程序进行解析     2.如果不能再本机解析查询, 可根据设定的查询DN ...

  2. 限制ssh远程登陆

    超过十次,就添加到hosts.deny里面去 #!/bin/bash date=`date +%Y%m%d` file="/var/log/secure" max=10 if [[ ...

  3. locust 参数,数据详解

    参数    说明-h, –help    查看帮助-H HOST, –host=HOST    指定被测试的主机,采用以格式:http://10.21.32.33–web-host=WEB_HOST  ...

  4. 前端MVC Vue2学习总结(六)——axios与跨域HTTP请求、Lodash工具库

    一.axios Vue更新到2.0之后宣告不再对vue-resource更新,推荐使用axios,axios是一个用于客户端与服务器通信的组件,axios 是一个基于Promise 用于浏览器和 no ...

  5. MySQL优化二 缓存参数优化

    数据库属于 IO密集型的应用程序,其主要职责就是数据的管理及存储工作.而我们知道,从内存中读取一个数据库的时间是微秒级别,而从一块普通硬盘上读取一个IO是在毫秒级别,二者相差3个数量级.所以,要优化数 ...

  6. Java的注释和Javadoc在eclipse生成的方法 – Break易站

    本文内容来自:Java的注释和Javadoc在eclipse生成的方法 – Break易站 1.  Java的注释 Java里有两种注释风格.下面这个写法是非常常见的 1 2 3 4 /*This i ...

  7. Android 中adb 命令(实用)

    1. 用命令的方式打开关闭mtklog adb  shell am broadcast -a com.mediatek.mtklogger.ADB_CMD -e cmd_name start/stop ...

  8. IIS 应用程序池自动停止

    IIS7 .NET Runtime version 2.0.50727.5420 - 执行引擎错误(000007FEE77AAF0E) (80131506) 装完系统,配置完IIS,发现.NET程序报 ...

  9. css 负边距

    负边距 可以改变 他在文档流中的尺寸 当块级元素设置 margin: -10px;  这个快 的大小没变但是他的定位的位置向上串了,压住了上面的文字   而且在他后面的文字  会爬到他身上 而前面的文 ...

  10. laravel ORM 命令2

    一.查询 获取数据库所有数据 Model::all() 获取指定条件数据 Modle::where('status','1')->get() 获取第一条数据 Model::where('stat ...