题目:

                                                                                                                                                         Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
     

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 - 1 or + 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,对任意的点坐标x每次有3种走法:x-1,x+1,2*x。每走一次花费时间为1minute,问从n到k最少需要花费多少时间?

      该题是最短路径问题,于是可以用BFS搜索,每次往三个方向BFS,直到到达k,每走一步记录当前时间。

//simonPR
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=;
int n,k,vis[maxn];
struct catche{
int site,times; //记录点和所花的时间。
};
queue<catche> q;
void init();
void init(){
while(!q.empty())
q.pop();
memset(vis,,sizeof(vis));
}
int bfs(int n,int k);
int bfs(int n,int k){
if(n==k) return ;
catche now,news,start;
start.site=n;
start.times=;
q.push(start); //将起点入队列。
vis[n]=;
while(!q.empty()){
int ts;
now=q.front();
q.pop();
for(int i=;i<;i++){ //分三个方向BFS。
if(i==) ts=now.site-;
else if(i==) ts=now.site+;
else ts=*now.site;
if(ts>maxn||vis[ts]==) continue; //如果已经访问过了或超出数据范围则跳过。
if(ts==k) return now.times+; //到达终点K,返回时间。
if(vis[ts]==) //更新点信息,并将新点入队列。
{
vis[ts]=;
news.site=ts;
news.times=now.times+;
q.push(news);
}
}
}
}
int main()
{
scanf("%d%d",&n,&k);
init(); //初始化。
int ans=bfs(n,k);
printf("%d\n",ans);
return ;
}
												

POJ-3278(BFS)的更多相关文章

  1. Catch That Cow POJ - 3278 bfs map超时,短路判断顺序。

    题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数. 坑:标题写了.有点不会写bfs了... ac代码 #define _CRT_SECURE_NO_WARNINGS #include& ...

  2. 【BFS】POJ 3278

    POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最 ...

  3. BFS POJ 3278 Catch That Cow

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

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

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

  5. catch that cow POJ 3278 搜索

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

  6. [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)

    BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...

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

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

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

  9. POJ 3278 Catch That Cow(模板——BFS)

    题目链接:http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a f ...

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

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

随机推荐

  1. VC自动与Internet时间服务器同步更新

    在VCKBASE.CSDN里挖了许久的坟,才找到一些有点用的资料,最后自己整理出这样的个函数,方面VC实现时间同步,多的不说,自己看源码,根据自己的需要可以适当修改源码: #include <W ...

  2. eclipse或IDEA连接魅蓝

    1.首先 安装ADB 驱动 http://developer.android.com/tools/device.html 如果没装就自行去下载安装 别的品牌都可以顺利连接,魅族手机特有的原因导至在开发 ...

  3. 启动redis出现Creating Server TCP listening socket *:6379: bind: No such file or directory

    E:\redis>redis-server.exe redis.windows.conf [8564] 10 Oct 20:00:36.745 # Creating Server TCP lis ...

  4. 你们都是怎么阅读android系统源码的,用什么工具?

    如果是eclipse,可以建立java或者c++工程,把framework源码导入.packages目录下的可以建立android工程.sourceinsight的话,建立工程把相应模块的代码添加进去 ...

  5. bzoj1858

    比较烦的线段树 首先询问3很弱智不说, 询问4以前做过类似的,好像是USACO月赛hotel那题类似,维护lmax,rmax,max三个域就可以了 操作0,操作1也很简单,仔细考虑一下就知道也是可以l ...

  6. POJ_3685_Matrix_(二分,查找第k大的值)

    描述 http://poj.org/problem?id=3685 一个n*n的矩阵,(i,j)的值为i*i+100000*i+j*j-100000*j+i*j,求第m小的值. Matrix Time ...

  7. 【转】c++重载、覆盖、隐藏——理不清的区别

    原文网址:http://blog.sina.com.cn/s/blog_492d601f0100jqqm.html 再次把林锐博士的<高质量c++编程指南>翻出来看的时候,再一次的觉得这是 ...

  8. Number Sequence ----HDOJ 1711

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. [GRYZ2014]最大连续子序列的和

    求给定序列的最大连续子序列和. 输入:第一行:n(n<100000)             第二行:n个整数[-3000,3000]. 输出:最大连续子序列的和. 样例: 输入: 7 -6 4 ...

  10. MySQL开启远程链接(2014.12.12)

    MySQL默认是关闭远程链接的,只能通过localhost访问本地数据库 如果不是本地访问就需要打开MySQL的远程连接: 基本步骤其实很简单: 1.进入mysql 2.依次运行下面的命令(黄色的为命 ...