Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 114140   Accepted: 35715

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

 
终于A掉这道题,BFS新认知。
 
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<vector>
#include<cmath>
#include<cstring>
#include<string> #define N 100010 using namespace std; void in(int &x){
register char c=getchar();x=0;int f=1;
while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
while(isdigit(c)){x=x*10+c-'0';c=getchar();}
x*=f;
} int a,b,ans;
struct Step{
int v,w;//位置,步数
Step(int xx,int s):v(xx),w(s){ }
};
bool vis[N];
queue<Step>Q; void BFS(){
memset(vis,0,sizeof(vis));
vis[a]=1;Q.push(Step(a,0));
while(!Q.empty()){
Step s=Q.front();Q.pop();
if(s.v==b) {
ans=s.w;break;
}
else {
if(s.v-1>=0 && !vis[s.v-1]){
Q.push(Step(s.v-1,s.w+1));
vis[s.v-1]=1;
}if(s.v+1<=N &&!vis[s.v+1]){
Q.push(Step(s.v+1,s.w+1));
vis[s.v+1]=1;
}if(s.v*2<=N && !vis[s.v*2]){
Q.push(Step(s.v*2,s.w+1));
vis[s.v*2]=1;
}
}
}
} int main()
{
in(a);in(b);
BFS();
printf("%d\n",ans);
return 0;
}

  

POJ3278——Catch That Cow的更多相关文章

  1. poj3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 73973   Accepted: 23308 ...

  2. POJ3278——Catch That Cow(BFS)

    Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...

  3. poj3278 Catch That Cow(简单的一维bfs)

    http://poj.org/problem?id=3278                                                                       ...

  4. POJ3278 Catch That Cow —— BFS

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  5. POJ3278 Catch That Cow(BFS)

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

  6. poj-3278 catch that cow(搜索题)

    题目描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...

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

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

  8. bfs—Catch That Cow—poj3278

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 87152   Accepted: 27344 ...

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

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

随机推荐

  1. LeetCode 438. Find All Anagrams in a String (在字符串中找到所有的变位词)

    Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...

  2. 【Spark】Stage生成和Stage源代码浅析

    引入 上一篇文章<DAGScheduler源代码浅析>中,介绍了handleJobSubmitted函数,它作为生成finalStage的重要函数存在.这一篇文章中,我将就DAGSched ...

  3. Vue相关开源项目库汇总 http://www.opendigg.com/tags/front-vue

    awesome-github-vue 是由OpenDigg整理并维护的Vue相关开源项目库集合.我们会定期同步OpenDigg上的项目到这里,也欢迎各位提交项目给我们. 如果收录的项目有错误,可以通过 ...

  4. 在Java中实现UDP协议编程(DatagramSocket/DatagramPacket)

    1.什么是UDP协议? UDP( User Datagram Protocol )协议是用户数据报,在网络中它与TCP协议一样用于处理数据包.在OSI模型中,在第四层——传输层,处于IP协议的上一层. ...

  5. 写web项目注意事项

    1.中文名2.文件存放路径(js css img)3.class详细路径(mydiv.myul li)

  6. B2242 [SDOI2011]计算器

    这个题就是把三个数论基础合在了一起,算是一道比较全面的题. 1的时候就是快速幂 2的时候是exgcd求逆元,特殊的,只有两数互质才有逆元. 3就是bsgs啦,还是不太熟 题干: Description ...

  7. 一、Linux文件权限与目录配置

    行文结构如下: 用户和用户组 Linux文件权限概念 Linux目录配置 重点回顾 1.用户与用户组 Linux是个多用户.多任务的系统,可能有多人同时使用这台机器进行工作,为了考虑每个人的隐私和工作 ...

  8. [App Store Connect帮助]三、管理 App 和版本(2.1)输入 App 信息:查看和编辑 App 信息

    在您添加 App 至您的帐户后,您也可以在“我的 App”部分查看和编辑 App 信息和平台版本信息. 在输入 App 信息前,请检查必填项.可本地化和可编辑属性.您在上传构建版本或提交您的 App ...

  9. 个人作业 - Alpha 项目测试

    写在前面 课程链接:https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业要求:https://edu.cnblogs.com ...

  10. $P5269 欧稳欧再次学车$

    \(problem\) 哇 看各位巨佬都来发\(T1\)的题解 我也来发一篇.(别的题目不会别瞎bb) 题目大意就是 \(T\) 秒 能走多少路程 第一行六个整数 \(T,N,L,R,X,K\) 接下 ...