Problem 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 X - 1 or X + 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

4HintThe 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

USACO 2007 Open Silver


思路

考验对状态的理解,每次走下一步有3种状态,bfs即可

代码

#include<bits/stdc++.h>
using namespace std;
const int d[3] = {1,-1,0};
struct node
{int pos;int step;
};
int n,k;
bool vis[200010];bool judge(int x)
{if(vis[x] || x<0 || x>100000)return false;return true;
}
int bfs(node st)
{queue<node> q;q.push(st);node next,now;memset(vis,false,sizeof(vis));vis[st.pos] = true;while(!q.empty()){now = q.front();q.pop();if(now.pos == k) return now.step;for(int i=0;i<3;i++){if(i==0 || i==1)next.pos = now.pos + d[i];elsenext.pos = now.pos * 2; next.step = now.step + 1; if(judge(next.pos)){q.push(next);vis[next.pos] = true;}}}
}int main()
{while(cin>>n>>k){node t;t.pos = n; t.step = 0;int ans = bfs(t);cout << ans << endl;}return 0;
}

转载于:https://www.cnblogs.com/MartinLwx/p/9903171.html

Hdoj 2717.Catch That Cow 题解相关推荐

  1. 杭电 2717 Catch That Cow

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

  2. hdu 2717 Catch That Cow

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

  3. POJ 3278 / hdu 2717 Catch That Cow (广搜)

    POJ 3278 HDU 2717 广搜题,用一个数组标记就可以过,不标记的话会超内存. 另外,poj的数据要比hdu强一些,比如0 100,这种数据.不特判的话会RE.不过如果不特判,在poj上用C ...

  4. HDU 2717 Catch That Cow(BFS)

    题目链接 好裸,BFS.杭电多组..2A.. 1 #include <stdio.h> 2 #include <string.h> 3 int p[100001],o[1000 ...

  5. Catch That Cow题解BFS方法C++实现

    题目描述 花花经过长时间的研究,终于研发出了能够跃迁的宇宙飞船.现在,他想要前往致远星.假设地球和致远星都在一个坐标轴上,其中地球位于坐标n,而致远星位于坐标k.而花花的飞船支持以下两种运动方式: 飞 ...

  6. c语言编辑87152,POJ 3287 (基础BFS) Catch That Cow

    这是做的第一道BFS,很基础很简单的题目 广度优先搜索算法如下:(用QUEUE) (1) 把初始节点S0放入Open表中: (2) 如果Open表为空,则问题无解,失败 退出: (3) 把Open表的 ...

  7. poj 3278 Catch That Cow 广搜

    hdu 2717 Catch That Cow,题目链接 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...

  8. 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)

    链接:https://ac.nowcoder.com/acm/contest/984/L 来源:牛客网 Catch That Cow 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3 ...

  9. 初识BFS POJ-3278 Catch That Cow FZU-2285 迷宫寻宝

    初识BFS POJ-3278 Catch That Cow FZU-2285 迷宫寻宝 令人窒息的创新实验课让我们写程序. POJ-3278 Catch That Cow Farmer John ha ...

最新文章

  1. caffeine_使用Caffeine和Spring Boot的多个缓存配置
  2. PAT甲级1068 Find More Coins (30 分):[C++题解]DP、背包问题、dp输出方案
  3. BZOJ.4553.[HEOI2016TJOI2016]序列(DP 树状数组套线段树/二维线段树(MLE) 动态开点)
  4. mysql定时增量备份_Mysql日常自动备份和增量备份脚本
  5. Spoon新建repository的时候
  6. Visual Studio 2013 (CV版)编译错误【error C4996: 'sprintf': This function or variable may be unsafe. 】的解决方案
  7. 运行python manage.py runserver报错现象、原因和解决办法
  8. 【干货笔记】CS224n-2019 学习笔记 Lecture 01 Introduction and Word Vectors
  9. 【图论】Dijkstra算法解决有向图最短路问题
  10. ea6500 v1 刷梅林_Linksys EA6500刷ddwrt成功记
  11. 神经网络学习小记录59——Pytorch搭建常见分类网络平台(VGG16、MobileNetV2、ResNet50)
  12. ios游戏使得newpad不断迅速增长
  13. IT前沿技术之node.js篇一:Node.js与javascript
  14. mac怎么搭建网站服务器,mac搭建本地服务器(示例代码)
  15. CSP-M2 B - HRZ 学英语
  16. 关于VCT(voxel cone trace——基于体素的锥形光线追踪)Renderer的代码解读
  17. SkyEye与Trace32集成并搭建调试环境
  18. 干货 :7招帮你打造完美数据科学GitHub作品集!
  19. 谷歌404页面html,简洁404页面HTML好看的404错误页源码
  20. 压力、焦虑还是抑郁? 正确诊断再治疗

热门文章

  1. 高速采集方案使利用UHF局部放电检测技术监控电网成为现实
  2. FGH60N60-ASEMI大功率IGBT管FGH60N60
  3. 基于微信小程序食堂订餐系统源码(微信小程序毕业设计)
  4. AES加密解密(java、web、app)
  5. 按什么键启用计算机管理,电脑结束任务按什么键
  6. html课程表斜杠,Word功课表斜线怎么做
  7. 上计算机课玩游戏检讨400字,上课说话检讨书400字(精选10篇)
  8. 字典树(Trie/前缀树)
  9. 离别信——CJ-麦科勒姆亲笔:致亲爱的波特兰
  10. IOS蓝牙4.0与读卡器通讯