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. Android Path路径设置,针对error opening trace file:No such file or directory

    对于android的开发者来说,首先要做的就是环境变量的配置.学习过java的人都知道,java是须要配置环境变量的,那么android开发是否也须要我们配置环境变量呢?当然,安卓的环境变量须要我们配 ...

  2. 从IBM的计划中分析出中国重新相当然的错误选择吗

    <IBM欲用物联网技术解决北京雾霾难题> http://security.zol.com.cn/469/4690141.html 读了上文.分析出的. 因为我没有太多的数据.不好分析. 有 ...

  3. LeetCode 246. Strobogrammatic Number (可颠倒数字) $

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  4. java及前端请求跨域问题

    主要代码:<meta http-equiv="Access-Control-Allow-Origin" content="*"> 说明一下什么情况下 ...

  5. Easyui 页面訪问慢解决方式,GZIP站点压缩加速优化

    1. 静态资源压缩GZIP是站点压缩加速的一种技术,对于开启后能够加快我们站点的打开速度.原理是经过server压缩,client浏览器高速解压的原理,能够大大降低了站点的流量. 详细代码能够參加je ...

  6. 换npm yarn的源让install超时去死吧

    安装npm install时,长时间停留在fetchMetadata: sill mapToRegistry uri http://registry.npmjs.org/whatwg-fetch处, ...

  7. [luoguP4142]洞穴遇险

    https://www.zybuluo.com/ysner/note/1240792 题面 戳我 解析 这种用来拼接的奇形怪状的东西,要不就是轮廓线\(DP\),要不就是网络流. 为了表示奇数点(即\ ...

  8. MySQL社区版是世界上最流行的开源数据库的免费

    昨天晚上搞了很久,终于搞清楚mysql的安装配置了,我真是太low了.当我在云服务器上登进Mysql时,真是高兴哈哈,咱一步一步来,彻底搞懂Mysql的安装配置. 我的安装环境: 阿里云服务器 1 2 ...

  9. 【WIP】Rails Client Side Document

    创建: 2017/09/15 更新: 2019/04/14 删除其他语言的表述 更新: 2017/10/14 标题加上[WIP]  引入JavaScrpit/CSS  manifesto  n. 货单 ...

  10. E20170617-hm

    notation   n. 记号,标记法; implicit   adj. 不言明[含蓄]的; 无疑问的,绝对的; 成为一部份的; 内含的; selector   n. 选择者,选择器; promot ...