Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 58072 | Accepted: 18061 |
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
K
Output
Sample Input
5 17
Sample Output
4
广度优先搜索#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std; const int Max=1100000;
struct Point
{
int x;
int num;
};
int N,K;
bool vis[210000];
void BFS()
{
queue<Point>a;
Point s,t;
memset(vis,false,sizeof(vis));
s.num=0;
s.x=N;
a.push(s);
vis[N]=true;
while(!a.empty())
{
s=a.front();
a.pop();
if(s.x==K)
{
printf("%d\n",s.num);
return ;
}
if(!vis[s.x+1]&&s.x+1<=K*2)
{
t.x=s.x+1;
t.num=s.num+1;
a.push(t);
vis[t.x]=true;
}
if(s.x-1>=0&&!vis[s.x-1])
{
t.x=s.x-1;
t.num=s.num+1;
a.push(t);
vis[t.x]=true;
}
if(s.x*2<=K*2&&!vis[s.x*2])
{
t.x=s.x*2;
t.num=s.num+1;
a.push(t);
vis[t.x]=true;
}
} }
int main()
{ scanf("%d %d",&N,&K);
BFS();
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏的更多相关文章
- Labeling Balls 分类: POJ 2015-07-28 19:47 10人阅读 评论(0) 收藏
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11893 Accepted: 3408 Descr ...
- Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 二分图匹配 分类: ACM TYPE 2014-10-01 19:57 94人阅读 评论(0) 收藏
#include<cstdio> #include<cstring> using namespace std; bool map[505][505]; int n, k; bo ...
- Can you find it? 分类: 二分查找 2015-06-10 19:55 5人阅读 评论(0) 收藏
Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need t ...
- Monthly Expense(二分) 分类: 二分查找 2015-06-06 00:31 10人阅读 评论(0) 收藏
Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...
- 菊花加载第三方--MBprogressHUD 分类: ios技术 2015-02-05 19:21 120人阅读 评论(0) 收藏
上次说到了网络请求AFN,那么我们在网络请求的时候,等待期间,为了让用户不认为是卡死或程序出错,一般都会放一个菊花加载,系统有一个菊花加载类叫UIProgressHUD.但是我今天要说的是一个替代它的 ...
- Rebuild my Ubuntu 分类: ubuntu shell 2014-11-08 18:23 193人阅读 评论(0) 收藏
全盘格式化,重装了Ubuntu和Windows,记录一下重新配置Ubuntu过程. //build-essential sudo apt-get install build-essential sud ...
- iOS开发网络数据之AFNetworking使用 分类: ios技术 2015-04-03 16:35 105人阅读 评论(0) 收藏
http网络库是集XML解析,Json解析,网络图片下载,plist解析,数据流请求操作,上传,下载,缓存等网络众多功能于一身的强大的类库.最新版本支持session,xctool单元测试.网络获取数 ...
- 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...
随机推荐
- Swift游戏实战-跑酷熊猫 00 游戏预览
这个系列我们将要一起来做一个这样的游戏
- JS练习题 显示登入者相关好友
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C++之路进阶——bzoj2199(奶牛议会)
F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser gryz2016 Logout 捐赠本站 Notice:由于本OJ ...
- cordova3.X 运用grunt生成plugin自定义插件骨架
Cordova提供了一组设备相关的API,通过这组API,移动应用能够以JavaScript访问原生的设备功能,如摄像头.麦克风等.Cordova还提供了一组统一的JavaScript类库,以及为这些 ...
- SharedPreferences 轻型的数据存储方式
//初始化,(名字,隐私或公开) SharedPreferences openTimes=getSharedPreferences("openTimes",0); //提交保存数据 ...
- paper 92:图像视觉博客资源2之MIT斯坦福CMU
收录的图像视觉(也包含机器学习等)领域的博客资源的第二部分,包含:美国MIT.斯坦福.CMU三所高校 1)这些名人大家一般都熟悉,本文仅收录了包含较多资料的个人博客,并且有不少更新,还有些名人由于分享 ...
- sklearn
Feature extraction - sklearn文本特征提取 http://blog.csdn.net/pipisorry/article/details/41957763 http://sc ...
- OpenStack 镜像制作
Contents [hide] 1 Centos6.5 img制作 1.1 基础环境安装 1.2 下载或从本地上传系统镜像 1.3 启动服务 1.4 建立镜像文件 1.5 通过virt-install ...
- web跨页弹窗选值
最近在项目中看到这样一种效果——点击当前网页文本框,然后弹出一个缩小的网页,并在网页里选择或填写数据,然后又返回当前网页,小网页关闭.感觉非常不错,其实在以前网上也看见过,只是当时没有留心.今天抽时间 ...
- 使用Application Loader打包上传AppStore流程
配置完你的证书,Bundle Identifier 和描述文件的配置 然后配置工程打开你项目工程 第一步,这里不能选择模拟器,选择iOS Device 如果不支持横屏,把这2个勾去掉 然后查看版本号和 ...