【BZOJ】1646: [Usaco2007 Open]Catch That Cow 抓住那只牛(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1646
这一题开始想到的是dfs啊,,但是本机测样例都已经re了。。。
那么考虑bfs。。。很巧妙?
首先我们得确定一个上下界。
当到达距离<0时显然不可能再走了(准确来说绝对不会比当前优),所以这里可以剪枝。
当到达距离>max(n, k)+1时也不能再走了(准确说不会比之前的优,比如说,你走到了k+2,但是之前就可在某个小于k的地方走×2的就走到了,那么就比这个少了1步)所以这里可以剪枝
然后bfs。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }
const int N=100005;
int n, k, front, tail, q[N], d[N], vis[N];
void add(int x) { if(vis[x]) return; vis[x]=1; q[tail++]=x; if(tail==N) tail=0; }
int main() {
read(n); read(k);
int mx=max(n, k)+1, t;
q[tail++]=n;
CC(d, 0x7f); d[n]=0;
while(front!=tail) {
t=q[front++]; if(front==N) front=0; vis[t]=0;
if(t==k) break;
int dt=d[t]+1;
if(t>0 && dt+1<d[t-1]) d[t-1]=dt, add(t-1);
if(t<mx && dt<d[t+1]) d[t+1]=dt, add(t+1);
if((t<<1)<=mx && dt<d[t<<1]) d[t<<1]=dt, add(t<<1);
}
print(d[k]);
return 0;
}
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
Farmer John starts at point 5 and the fugitive cow is at point 17.
Sample Output
OUTPUT DETAILS:
The 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.
HINT
Source
【BZOJ】1646: [Usaco2007 Open]Catch That Cow 抓住那只牛(bfs)的更多相关文章
- BZOJ 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛( BFS )
BFS... -------------------------------------------------------------------------------------------- ...
- bzoj 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛【bfs】
满脑子dp简直魔性 模拟题意bfs转移即可 #include<iostream> #include<cstdio> #include<queue> using na ...
- BZOJ1646: [Usaco2007 Open]Catch That Cow 抓住那只牛
1646: [Usaco2007 Open]Catch That Cow 抓住那只牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 634 Solved ...
- 2014.6.14模拟赛【bzoj1646】[Usaco2007 Open]Catch That Cow 抓住那只牛
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- 【题解】[Usaco2007 Open]Catch That Cow 抓住那只牛-C++
题目DescriptionFarmer John has been informed of the location of a fugitive cow and wants to catch her ...
- 抓住那只牛!Catch That Cow POJ-3278 BFS
题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...
- hdu 2717 Catch That Cow(广搜bfs)
题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...
- 【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)
Catch That Cow Descriptions: Farmer John has been informed of the location of a fugitive cow and wan ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
随机推荐
- GMM高斯混合模型 学习(2)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHpxMjAwODExMjExMDc=/font/5a6L5L2T/fontsize/400/fill/I0 ...
- Docker的安装使用-第1章
Docker的安装 1.1 环境说明 操作系统: Red Hat Enterprise Linux Server release 7.1 软件环境: 系统已经配置了yum安装源 软件版本: docke ...
- Spring velocity 中文乱码 解决方案
主要有这么几步,在spring web 的 [sevlet-name]-servlet.xml文件中,修改为: 黑体字体为关键,其它根据你的实际情况配置: <!-- ============= ...
- Android编程的写法规范
一.获取View对象 1.private EditText m_txtSmsPhone = null;//定义一个EditText控件的调用对象 m_表示为View对象 txt表示文本框 2.在pro ...
- 函数适配器bind2nd 、mem_fun_ref 源码分析、函数适配器应用举例
一.适配器 三种类型的适配器: 容器适配器:用来扩展7种基本容器,利用基本容器扩展形成了栈.队列和优先级队列 迭代器适配器:(反向迭代器.插入迭代器.IO流迭代器) 函数适配器:函数适配器能够将仿函数 ...
- VC++编写ActiveX控件
ActiveX这门技术是通过生成“*.ocx”文件来实现的.先来了解下OCX文件,在百度百科上面对OCX是这样解释的:“.ocx是ocx控件的扩展名,OCX 是对象类别扩充组件.如果你用过Visual ...
- 网站收到的url请求链接,字母全部变为小写
http://www.ithao123.cn/content-5360465.html
- mybatis 参数说明
1 简单参数 <select id="selectUsers" resultType="User"> select id, username, pa ...
- FTL页面常用到的一些方法combobox、combotree、datagrid
参考文件:点击下载 1.combobox: (1).js 1)初始化combobox //相似度 $('#same').combobox({ //url:"<@s.url value= ...
- GameObject非激活状态,触发测试
测试结果为OnEnable,Start不执行.Awake不管激活未激活都执行 不管是驻留在层级面板,还是手动拖上去. 但是,在任何时间设为激活.就会2个都触发.Awake和Start只触发一次.这个问 ...