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

发现广度优先搜索适合什么题呢?就是那种在每个点都给你几个选择,然后问你最短路径的问题,对,就是这样,这样的题目最适合广度优先搜索。

这次的这个就是,每次Farmer有三个选择,然后求最短到达目的地的行程。简直是广搜的模板题。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int color[1000005];
int dis[1000005]; queue<int> q; int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int N,K;
cin>>N>>K; if(N==K)
{
cout<<0<<endl;
}
else
{
memset(color,0,sizeof(color));
memset(dis,0,sizeof(dis)); q.push(N);
while(!q.empty())
{
N=q.front();
q.pop();
if(N-1==K || N+1==K || 2*N==K)
{
cout<<dis[N]+1<<endl;
break;
} if(color[N-1]==0 && N-1>=0)
{
color[N-1]=1;
dis[N-1]=dis[N]+1;
q.push(N-1);
}
if(color[N+1]==0)
{
color[N+1]=1;
dis[N+1]=dis[N]+1;
q.push(N+1);
}
if(color[2*N]==0 && 2*N<=100000)
{
color[2*N]=1;
dis[2*N]=dis[N]+1;
q.push(2*N);
}
}
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3278:Catch That Cow的更多相关文章

  1. POJ 3278: Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 44613   Accepted: 13946 ...

  2. POJ 3278:The merchant(LCA&DP)

    The merchant Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6864   Accepted: 2375 Desc ...

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

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

  4. 抓住那只牛!Catch That Cow POJ-3278 BFS

    题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...

  5. BFS POJ 3278 Catch That Cow

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

  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(赶牛行动)

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

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

  9. catch that cow POJ 3278 搜索

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

随机推荐

  1. suse下静默方式安装oracle(无图形界面)

    以 http://www.cnblogs.com/0201zcr/p/4728241.html 为主 以 http://www.cnblogs.com/jyzhao/p/5001782.html 为参 ...

  2. 微信小程序循环中点击一个元素,其他的元素不发生变化,类似点击一个循环中的语音,其他的不发生点击事件

    类似语音,因为都在一个数据内,所以点击第一个,所有的语音都变化,解决方法就是 把整个数据都获取下来,然后更改其中一个需要更改的值,然后再把整个数据都setdata回去,如果需要动画的话,wxml里面放 ...

  3. shiro#springboot

    1. shiro的使用围绕着securityManager,权限需要从realm中来. securityManager可以设置realm或者realms,或者通过设置authenticator来设置r ...

  4. 看完本文,Essay写作再也不需要凑字数

    很多同学都说过自己写论文的时候出现“词穷”的情况,无奈只能靠“胡编乱造”来凑字数写出开头段,这其实是大家的阅读量没有达到要求.但不能因为出现这种情况就对自己的论文不负责任,否则你的论文分数可能就不会对 ...

  5. JAVA CookieUtil

    package com.itheima.shop.utils; import java.io.UnsupportedEncodingException; import java.net.URLDeco ...

  6. 课程报名 | 基于模型训练平台快速打造 AI 能力

    我们常说的 AI 通用能力往往不针对具体的行业应用,而是主要解决日常或者泛化的问题,很多技术企业给出的方案是通用式的,比如通用文字识别,无论识别身份证.驾驶证.行驶证等,任何一张图片训练后的模型都会尽 ...

  7. swarm docker-compose.yml NFS 搭建LNMP

    列表: 172.16.0.40     (swarm-master) 172.16.0.160     (swarm-worker) 172.16.0.170     (swarm-worker) 1 ...

  8. Linux每日练习-awk命令的内外部变量传递20200225

  9. 023-PHP常用数组函数

    <?php $colors = array("red", "green", "blue","yellow"); p ...

  10. 一条 SQL 在 Apache Spark 之旅

    转载自过往记忆大数据 https://www.iteblog.com/archives/2561.html Spark SQL 是 Spark 众多组件中技术最复杂的组件之一,它同时支持 SQL 查询 ...