E - Catch That Cow
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
Output
Sample Input
- 5 17
Sample Output
- 4
Hint
- #include <cstdio>
- #include <queue>
- #define MAX 100001
- using namespace std;
- int n, m;
- int ret[MAX], vis[MAX] = {0};
- queue<int> q;
- int bfs(int n, int m)
- {
- if(n == m)
- return 0;
- int cur;
- q.push(n);
- while(!q.empty())
- {
- cur = q.front();
- q.pop();
- if(cur + 1 < MAX && !vis[cur+1])
- {
- ret[cur+1] = ret[cur] + 1;
- vis[cur+1] = 1;
- q.push(cur+1);
- }
- if(cur + 1 == m)
- break;
- if(cur - 1 >= 0 && !vis[cur-1])
- {
- ret[cur-1] = ret[cur]+1;
- vis[cur-1] = 1;
- q.push(cur-1);
- }
- if(cur - 1 == m)
- break;
- if(cur * 2 < MAX && !vis[cur*2])
- {
- ret[cur*2] = ret[cur] + 1;
- vis[cur*2] = 1;
- q.push(cur*2);
- }
- if(cur * 2 == m)
- break;
- }
- return ret[m];
- }
- int main()
- {
- scanf("%d%d", &n, &m);
- printf("%d\n", bfs(n, m));
- return 0;
- }
E - Catch That Cow的更多相关文章
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- poj3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 73973 Accepted: 23308 ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- 2016HUAS暑假集训训练题 B - Catch That Cow
B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 58072 Accepted: 18061 ...
- [HDOJ2717]Catch That Cow
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
随机推荐
- 快排 quicksort 快速排序
首先看这个 http://www.cnblogs.com/morewindows/archive/2011/08/13/2137415.html 看完后就没有什么不理解的了... PS 注意 凡是在一 ...
- Office 2013
Microsoft Office 2013 Professional Plus 32位简体中文版文件名: SW_DVD5_Office_Professional_Plus_2013_W32_ChnSi ...
- 打印NxN的矩阵
找出规律,并打印出一个NxN的矩阵,规律就是从首坐标开始顺时针依次增大: #include<iostream> #include<vector> using namespace ...
- SQLServer 2008 :error 40 出现连接错误
在与SQLServer建立连接时出现与网络相关的或特定与实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且SQL SERVER已配置允许远程链接.(provide:命名管道提供程序,e ...
- Android设置Activity背景为透明style
方法一: 通过Theme.Translucent @android:style/Theme.Translucent @android:style/Theme.Translucent.NoTitleBa ...
- 优化器的使用oracle ---explain plan
如果要分析某条SQL的性能问题,通常我们要先看SQL的执行计划,看看SQL的每一步执行是否存在问题. 如果一条SQL平时执行的好好的,却有一天突然性能很差,如果排除了系统资源和阻塞的原因,那么基本可以 ...
- ASP.NET -- repeater控件的使用
转载自网络-原网址[http://blog.csdn.net/haitaodoit/article/details/7021214] repeater绑定数据: protected void Page ...
- 我的IOS学习之路(三):手势识别器
在iOS的学习中,对于手势的处理是极为重要的,如对于图片,我们经常需要进行旋转,缩放以及移动等.这里做一下总结,详见代码. - (void)viewDidLoad { [super viewDidLo ...
- WPF 实现的等待效果界面
这个界面的效果是从WinForm 转变过来,可以实现等待的效果,操作完成以后就自动关掉. 效果图如下 有一个动态的手机等待效果的样式,中间的文字可以自己定义,提供了方法可以修改中间"正在加载 ...
- QT中QWidget、QDialog及QMainWindow的区别
本文转自http://www.cnblogs.com/aqxin/archive/2011/05/23/2054156.html QWidget类是所有用户界面对象的基类. 窗口部件是用户界面的一个基 ...