POJ 3278 Catch That Cow(bfs)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 80273 | Accepted: 25290 |
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
思路
题意:给定起点和终点,有三种走的方式,假设当前为 now,那么可以选择下一次到达 now - 1 或 now + 1 或 2*now,问起点到终点最少需要几步。
题解:bfs,下一个状态即为三种可选择的位置。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 100005; int step[maxn],que[maxn],vis[maxn]; int bfs(int st,int ed) { int E = 0,F = 0; que[F++] = st; vis[st] = 1; for (;;) { int now = que[E]; if (now == ed) return step[now]; if (now + 1 < maxn && !vis[now + 1]) step[now + 1] = step[now] + 1,vis[now + 1] = 1,que[F++] = now + 1; if (now - 1 >= 0 &&!vis[now - 1]) step[now - 1] = step[now] + 1,vis[now - 1] = 1,que[F++] = now - 1; if (2*now < maxn && !vis[2*now]) step[2*now] = step[now] + 1,vis[2*now] = 1,que[F++] = 2 * now; E++; } } int main() { int N,K; memset(vis,0,sizeof(vis)); scanf("%d%d",&N,&K); if (N >= K) printf("%d\n", N - K); else printf("%d\n",bfs(N,K)); return 0; }
POJ 3278 Catch That Cow(bfs)的更多相关文章
- poj 3278 Catch That Cow (bfs)
题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...
- POJ 3278 Catch That Cow(BFS 剪枝)
题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出 ...
- POJ——3278 Catch That Cow(BFS队列)
相比于POJ2251的三维BFS,这道题做法思路完全相同且过程更加简单,也不需要用结构体,check只要判断vis和左右边界的越界情况就OK. 记得清空队列,其他没什么好说的. #include< ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- poj 3278(hdu 2717) Catch That Cow(bfs)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- POJ 3278 Catch That Cow(求助大佬)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 109702 Accepted: 34255 ...
- 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 ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
随机推荐
- JqueryDataTable的使用(.Net平台)
上一篇随笔提到了MvcPager,最近用到了一款前端JQ插件------DataTable(简称DT),很好用. DT是一款前端插件,和后端完全分离开,就这点来看,我就特别喜欢. 一.使用DT,需要以 ...
- 你必须收藏的Github技巧
一秒钟把Github项目变成前端网站 GitHub Pages大家可能都知道,常用的做法,是建立一个gh-pages的分支,通过setting里的设置的GitHub Pages模块可以自动创建该项目的 ...
- [deviceone开发]-openPage的动画效果示例
一.简介do_App的openPage支持16种过场动画,这个示例直观的展示16种动画的效果.适合初学者.二.效果图三.相关下载https://github.com/do-project/code4d ...
- Atitit 《控制论原理与概论attilax总结
Atitit <控制论原理与概论attilax总结 <控制论> 奠基之作,出自创始人维纳.虽然内容权威,但我认为带有相当强烈的个人色彩,且门槛较高,不适合入门.深入研究控制论必看书籍 ...
- unable to boot the simulator,无法启动模拟器已解决
突然模拟器报错:unable to boot the simulator(无法启动模拟器) 试了好几种解决办法,删除所有的模拟器重启以后再添加,删除钥匙串登陆中的证书,重新安装Xcode都不行 最后通 ...
- iOS10 适配问题-Xcode8
前段时间升级了Xcode8,整体来说对OC的影响不大,但是还是跳一个坑,消耗了不少时间.这里总结下遇到的适配问题. 1.权限问题 Xcode8 访问相机.相册等需要权限的地方崩溃 解决办法: 在使用私 ...
- Java中一些常用的方法
1.计算程序运行时常 long start = System.currentTimeMillis(); … … … long end = System.currentTimeMillis(); Sys ...
- 后记:IT软件人员学习的书籍 - IT软件人员书籍系列文章
1年了,软件人员学习书籍系列总算是写完了.虽然文字篇幅不多,主要对各个角色的一些基本内容做了介绍,但是更重要的是能够提供相关的人员学习书籍进行下载,让更多的人能够从中学习到更多的知识. 这个系列,从项 ...
- sipex3232上电发送输入端拉低问题
之前没怎么注意,在使用下载盒通过isp下载stm32时,由于是通过stm32板子给下载盒供电,所以是我的stm32板子和下载盒同时上电.这样总是不能够进入下载模式,后来对下载盒进行仿真,发现在下载发送 ...
- NopCommerce 在Category 显示 Store List列表
实现效果如下: 1.在前台Web的Category Menu显示 Store; 2.点击 Store 显示 Store List列表: 3.点击 列表Store 的 Company Name 进入该S ...