【题解】[Usaco2007 Open]Catch That Cow 抓住那只牛-C++
题目
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?
农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上幽发,尽快把那只奶牛抓回来,他们都站在数轴上.约翰在N(O≤N≤100000)处,奶牛在K(O≤K≤100000)处.约翰有两种办法移动,步行和瞬移:步行每秒种可以让约翰从x处走到x+1或x-1处;而瞬移则可让他在1秒内从x处消失,在2x处出现.然而那只逃逸的奶牛,悲剧地没有发现自己的处境多么糟糕,正站在那儿一动不动.那么,约翰需要多少时间抓住那只牛呢?
Input
Line 1: Two space-separated integers: N and K
仅有两个整数N和K.
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
最短的时间.
Sample Input
5 17
Farmer John starts at point 5 and the fugitive cow is at point 17.
Sample Output
4
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.
思路
这道题依然是一个BFS,可以拓展三种状态:-1,+1或*2,但是要添加一些判断使得不会让标记数组越界导致RE(我死了很多次!!!)
其他的就是模板了…
代码
#include<bits/stdc++.h>
using namespace std;
int n,k;
bool vis[];
struct node
{
int x,s;
};
void bfs()
{
queue<node> q;
vis[n]=;
int tx;
q.push((node){n,});
while(!q.empty())
{
node now=q.front();
//q.pop();
if(now.x==k)
{
cout<<now.s<<endl;
exit();
}
tx=now.x-;
if(tx>=)
if(!vis[tx])
{
q.push((node){tx,now.s+});
}
if(tx>*k)continue;
tx=now.x+;
if(!vis[tx])
{
q.push((node){tx,now.s+});
vis[tx]=;
}
tx=now.x*;
if(!vis[tx])
{
q.push((node){tx,now.s+});
vis[tx]=;
}
}
}
int main()
{
cin>>n>>k;
bfs();
cout<<"这组数据无解"<<endl;//打着玩的
return ;
}
【题解】[Usaco2007 Open]Catch That Cow 抓住那只牛-C++的更多相关文章
- 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... -------------------------------------------------------------------------------------------- ...
- 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 ...
- 【BZOJ】1646: [Usaco2007 Open]Catch That Cow 抓住那只牛(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1646 这一题开始想到的是dfs啊,,但是本机测样例都已经re了... 那么考虑bfs...很巧妙? ...
- bzoj 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛【bfs】
满脑子dp简直魔性 模拟题意bfs转移即可 #include<iostream> #include<cstdio> #include<queue> using na ...
- 抓住那只牛!Catch That Cow POJ-3278 BFS
题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
随机推荐
- ARTS第九周打卡
Algorithm : 做一个 leetcode 的算法题 /* 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. ...
- 机器学习笔记--Hoeffding霍夫丁不等式
Hoeffding霍夫丁不等式 在<>第八章"集成学习"部分, 考虑二分类问题\(y \in \{-1, +1\}\) 和真实函数\(f\), 假定基分类器的错误率为\ ...
- double check
http://www.cnblogs.com/limingluzhu/p/5156659.html http://blog.csdn.net/chenchaofuck1/article/details ...
- vuex 理解
为什么要用vuex?页面由多个视图组成,用户操作会引视图的状态变化. 多个视图依赖于同一状态(例如:菜单导航) 来自不同视图的行为需要变更同一状态(例如:评论弹幕) vuex 的作用 为vue.js开 ...
- metronic-v4.6 使用经验
1.弹框居中显示 上下居中 需要上下居中引用 bootstrap-modalmanager.js 左右居中 修改 bootstrap-modal.js 中 this.$element.css('ma ...
- 帝国cms“建立目录不成功,请检查目录权限”的解决方法
就这个看似简单的问题我折腾了两天,百度看产生这个问题的原因有很多也很宽泛,大部分说的是初始化内置数据,但我出现“建立目录不成功,请检查目录权限”的原因估计只有少部分人会遇到. 内置初始化数据是你上传文 ...
- mac OS下 安装MySQL 5.7
Mac OS X 下 TAR.GZ 方式安装 MySQL 5.7 与 MySQL 5.6 相比, 5.7 版本在安装时有两处不同: 1:初始化方式改变, 从scripts/mysql_install_ ...
- 关于微信小程序发布新版本后的提示用户更新的方法详解
当小程序发布新的版本后 ,用户如果之前访问过该小程序,通过已打开的小程序进入(未手动删除),则会检测新版本,提醒用户更新新的版本 话不多说,上代码 App({ onLaunch: function ( ...
- java轻松玩转httpget和httppost
废话不多少说,直接上代码 //get请求 public static void HttpClientGet(String url) throws Exception { // 获取http客户端 Cl ...
- weblogic快速安装版的安装和使用
weblogic 12c提供了一个225MB大小的zip包, 我们下载下来之后解压后, 到jdk的bin目录下面, 执行java -jar ORACLE_HOME=[webloigc安装jar包绝对路 ...