B - Catch That Cow (抓牛)
B - Catch That Cow
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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 <stdio.h>
#include <string.h>
#include <queue>
using namespace std; const int N = ;
int map[N+];
int n,k; struct node
{
int x,step;
}; int check(int x)
{
if(x< || x>=N || map[x])
return ;
return ;
} int bfs(int x)
{
queue <node> Q;
node a,next; a.x = x;
a.step = ;
map[x] = ;
Q.push(a); while(!Q.empty())
{
a = Q.front();
Q.pop(); if(a.x == k)
return a.step;
next = a;
//每次都将三种状况加入队列之中
next.x = a.x+;
if(check(next.x))
{
next.step = a.step+;
map[next.x] = ;
Q.push(next);
}
next.x = a.x-;
if(check(next.x))
{
next.step = a.step+;
map[next.x] = ;
Q.push(next);
}
next.x = a.x*;
if(check(next.x))
{
next.step = a.step+;
map[next.x] = ;
Q.push(next);
}
}
return ;
} int main()
{
int ans;
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(map,,sizeof(map));
if (n>k) ans=n-k;
else ans = bfs(n);
printf("%d\n",ans);
}
return ;
}
B - Catch That Cow (抓牛)的更多相关文章
- BZOJ1646: [Usaco2007 Open]Catch That Cow 抓住那只牛
1646: [Usaco2007 Open]Catch That Cow 抓住那只牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 634 Solved ...
- BZOJ 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛( BFS )
BFS... -------------------------------------------------------------------------------------------- ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- 抓住那只牛!Catch That Cow POJ-3278 BFS
题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...
- 牛客假日团队赛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 ...
- 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 ...
- ***参考Catch That Cow(BFS)
Catch That Cow Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
随机推荐
- react-native 模仿原生 实现下拉刷新/上拉加载更多(RefreshListView)
1.下拉刷新/上拉加载更多 组件(RefreshListView) src/components/RefreshListView/index.js /** * 下拉刷新/上拉加载更多 组件(Refre ...
- openerp config file
[options] addons_path = /bin/openerp/addonsadmin_passwd = admincsv_internal_sep = , db_host = False ...
- Notepad++输入模式之改动模式、插入模式
notepad++光标是直的,怎样让它变成竖的? 通常光标是竖的.为插入模式,光标在字符之间时输入,内容会随输入的内容向后移动,新输入的内容不会替换后面的内容. 当按一下"Insert&qu ...
- Visual studio C++ MFC之列表控件CListCtrl Control
背景 本篇旨在MSDN帮助文档下总结列表控件CListCtrl Control的使用,并列出碰到的具体问题. 正文 列表型控件List Control的类是ClistCtrl,具体成员对象详见链接,以 ...
- jQuery 文档操作 - prependTo() ,appendTo()方法
其他jquery文档操作方法:http://www.w3school.com.cn/jquery/jquery_ref_manipulation.asp jQuery 参考手册 - 文档操作 appe ...
- 你应该将应用迁移到Spring 4的五个原因
本文来源于我在InfoQ中文站翻译的文章,原文地址是:http://www.infoq.com/cn/news/2015/12/five-reasons-to-migrate-spring4 Rafa ...
- NIO之阻塞IO与非阻塞IO(包含Selector使用)
阻塞IO 传统的 IO 流都是阻塞式的. 也就是说,当一个线程调用 read() 或 write()时,该线程被阻塞,直到有一些数据被读取或写入,该线程在此期间不能执行其他任务. 因此,在完成网络通信 ...
- Aurora学习笔记连载一:仿真平台搭建
由于公司项目需要,需要学习Aurora协议,才有了这样的连载学习笔记,也算是对自己学习的一份记录吧. 对于Aurora是什么,大家自行百度. 当然,Kevin也在此先提醒大家,本套学习笔记不是你想学就 ...
- C#微信公众号学习 - (一)测试账号申请
主要分为两部分: 1.创建C#的项目,并发布, 2.微信填写发布的地址进行开发者验证. 一. VS环境为VS2017,创建项目时,自带的一些东西发布会导致各种错误,无奈,创建了空项目aspx窗体,如下 ...
- C++语言基础(10)-虚继承
一.产生背景 先看下列一份代码: //间接基类A class A{ protected: int m_a; }; //直接基类B class B: public A{ protected: int m ...