【题解】[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 ...
随机推荐
- Django多对多关系建立及Form组件
目录 Django多对多关系 1.创建方式一全自动 2.创建方式二纯手撸 3.半自动(推荐使用) forms校验组件 使用forms组件实现注册功能 form常用字段和插件 数据校验 钩子函数 HOO ...
- python 手机App数据抓取实战一
前言 当前手机使用成为互联网主流,每天手机App产生大量数据,学习爬虫的人也不能只会爬取网页数据,我们需要学习如何从手机 APP 中获取数据,本文就以豆果美食为例,讲诉爬取手机App的流程 环境准备 ...
- python爬虫框架scrapy 豆瓣实战
Scrapy 官方介绍是 An open source and collaborative framework for extracting the data you need from websit ...
- js — 对象
目录 1. 字符串和数值之间转换 2. 对象(object) 3. 日期对象 4. 数学Math对象 5. 流程控制 6. 循环 1. 字符串和数值之间转换 1.字符串转数值 var str = '1 ...
- Linux驱动函数解读
一.kmalloc().kzalloc()和vmalloc() 这三个函数都可以分配连续的虚拟内存 除此之外,这三个函数的区别有: 1. kmalloc()和kzalloc()函数分配的物理内存也是连 ...
- Dubbo#编译动态扩展类
这篇排版有问题 后面修改....**** 以ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();为例 - ...
- jq获取元素偏移量offset()
解释: 1 获取匹配元素在当前视口的相对偏移. 2 返回的对象包含两个整型属性:top 和 left demo1: 获取top与left var aaa = $(".aaa "); ...
- Markdown中有序列表和无序列表
最近有用户问我,在简书写 Markdown, 一条有序列表 item 之后接一条无序列表 item,为什么 parse 的结果,第二个 item 依旧是作为有序列表的第二项显示,带有有序列表的列表符号 ...
- 19-MySQL DBA笔记-操作系统、硬件、网络的优化
第19章 操作系统.硬件.网络的优化 本章将介绍操作系统和硬件的性能优化,对于硬件,我们主要讲述CPU.内存.磁盘阵列及固态硬盘.任何优化,首先都需要有足够的数据支持,对于操作系统下性能数据的收集,这 ...
- (六)springmvc之ModelAndView、Model、Map、ModelMap
<a href="<%=request.getContextPath()%>/responseData/response_1">使用原生的作用域</a ...