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

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

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.

Source

 
  简单一维广搜
  题意
  你在一个一维坐标的n位置,牛在k位置,你要从n到k,抓到那头牛。你可以有三种走法,n+1,n-1,或者n*2直接跳。求你抓到那头牛的最短步数。
  思路
  简单广搜的思想。状态跳转的时候有三种跳转的方式,将新的状态放到队列中,再不断提取队列中最前面的状态,直到找到k位置。
  代码
 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; bool isw[]; struct Node{
int x;
int s;
}; bool judge(int x)
{
if(x< || x>)
return true;
if(isw[x])
return true;
return false;
} int bfs(int sta,int end)
{
queue <Node> q;
Node cur,next;
cur.x = sta;
cur.s = ;
isw[cur.x] = true;
q.push(cur);
while(!q.empty()){
cur = q.front();
q.pop();
if(cur.x==end)
return cur.s;
//前后一个个走
int nx;
nx = cur.x+;
if(!judge(nx)){
next.x = nx;
next.s = cur.s + ;
isw[next.x] = true;
q.push(next);
}
nx = cur.x-;
if(!judge(nx)){
next.x = nx;
next.s = cur.s + ;
isw[next.x] = true;
q.push(next);
}
//向前跳
nx = cur.x*;
if(!judge(nx)){
next.x = nx;
next.s = cur.s + ;
isw[next.x] = true;
q.push(next);
}
}
return ;
} int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF){
memset(isw,,sizeof(isw));
int step = bfs(n,k);
printf("%d\n",step);
}
return ;
}

Freecode : www.cnblogs.com/yym2013

poj 3278:Catch That Cow(简单一维广搜)的更多相关文章

  1. POJ - 3278 Catch That Cow 简单搜索

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

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

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

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

  6. poj 3278 Catch That Cow (广搜,简单)

    题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...

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

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

  8. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

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

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

随机推荐

  1. CCF 模拟C 找最大矩形+输入输出外挂

    http://115.28.138.223:81/view.page?opid=3 统计出连续的最长乘以当前高度,找最大即可 #include<iostream> #include< ...

  2. COGS 2421.[HZOI 2016]简单的Treap 题解

    题目大意: 给定n个数及其优先级,求对应的符合最小堆性质的Treap的先序遍历. n<=500000. 解法: 目前为止我只想到了三种解法,其中第三种是正解. 1.暴力1 以优先级为关键字排序, ...

  3. centos 随意截屏

    yum install kdegraphics 菜单路径: applications(应用程序)/Graphics(图形图像)/KSnapshot

  4. 5. apktool 给XX手机卫士加广告页

    一. 编写广告页 写一个广告页面,并调用其他页面的demo (1) 设计界面如下 (2) 编写代码如下 public class SplashActivity extends Activity {   ...

  5. Eclipse J2EE LUNA 部署tomcat

  6. mysql中You can't specify target table for update in FROM clause错误

    原SQL delete from DEP_SYSTEM_PORTLET_SETTINGS where ID in ( select ID from DEP_SYSTEM_PORTLET_SETTING ...

  7. 善用VS中的Code Snippet来提高开发效率

    http://www.cnblogs.com/anderslly/archive/2009/02/16/vs2008-code-snippets.html http://www.cnblogs.com ...

  8. C# 毕业证书打印《三》

    打印很关键的方法,打印方法DataPrint(),将你要打印的数据信息发送到打印机就可以了,打印机将自动处理. public void DataPrint() { try { PrintDocumen ...

  9. Linux下安装Django

    Django是基于Python开发的免费的开源网站框架,也是python web开发中重量级的web框架,可以用于快速搭建高性能并且优雅的网站! 下面以Fedora为例安装Django,最新Fedor ...

  10. 7.nodejs权威指南--加密与压缩

    1. 加密与压缩 1.1 加密 var crypto = require('crypto'); var text = "12345678"; var hasher = crypto ...