【题解】[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 ...
随机推荐
- Laravel安装和composer安装
下载地址:https://getcomposer.org/download/ 他会自动找到你的php目录,如果没有记得手动修改 一直点下一步,即可. 如果安装不成功,可能是之前安装过composer ...
- Django 1.8.2 admin 数据库操作按下保存按钮出错
Django报错:Runtimeerror: generator raised StopIteration python版本太新不兼容照成,下载python3.6就行了
- MySQL 解决source 命令导入数据库 乱码
在我把库.表.sql脚本的编码格式都设置为UTF-8后,任然有乱码,任然有报错: 于是按以下方式重新登录后,解决: mysql -u root -p --default-character-set=u ...
- SpinWait
其实SpinWait的code 非常简单,以前看过很多遍,但是从来都没有整理过,整理也是再次学习吧. 我们先看看SpinWait的一些评论或者注意点吧:如果等待某个条件满足需要的时间很短,而且不希望发 ...
- (八) Hibernate中的Session以及事务
HibernateUtil.getSessionFactory().getCurrentSession() 和HibernateUtil.getSession() 的区别: 1.异:getCurren ...
- (十)mybatis之缓存
一.缓存的意义 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)去查询,从缓存中进行查询,从而提高查询效率,解决了高并发系统的性能问题. 二.mybatis ...
- (十)shiro之自定义Realm以及自定义Realm在web的应用demo
数据库设计 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/ ...
- Java链表设计
链表 1,链表的实现 在实际开发之中对象数组是一项非常实用的技术,并且利用其可以描述出“多”方的概念,例如:一个人有多本书,则在人的类里面一定要提供有一个对象数组保存书的信息,但是传统的对象数组依赖于 ...
- 【原创】大叔经验分享(90)linux服务器iowait和负载很高
# top top - 21:21:51 up 207 days, 1:30, 5 users, load average: 0.90, 0.79, 1.62 Tasks: 249 total, 1 ...
- input 默认提示文字 样式修改(颜色,大小,位置)
input::-webkit-input-placeholder{ color:red; font-size:20px; ...... }