题目
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++的更多相关文章

  1. BZOJ1646: [Usaco2007 Open]Catch That Cow 抓住那只牛

    1646: [Usaco2007 Open]Catch That Cow 抓住那只牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 634  Solved ...

  2. BZOJ 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛( BFS )

    BFS... -------------------------------------------------------------------------------------------- ...

  3. 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 ...

  4. 【BZOJ】1646: [Usaco2007 Open]Catch That Cow 抓住那只牛(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1646 这一题开始想到的是dfs啊,,但是本机测样例都已经re了... 那么考虑bfs...很巧妙? ...

  5. bzoj 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛【bfs】

    满脑子dp简直魔性 模拟题意bfs转移即可 #include<iostream> #include<cstdio> #include<queue> using na ...

  6. 抓住那只牛!Catch That Cow POJ-3278 BFS

    题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...

  7. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  8. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  9. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

随机推荐

  1. Calibre 和 Kindle 配合的使用方法

    1. 前言 使用"Calibre"软件,把网上下载的电子书转换成适合kindle阅读的格式. 2. 使用经验总结 2.1 首行缩进.段落间距设置 2.2 输出为mobi格式设置 2 ...

  2. Excel计算、统计函数

    Excel计算.统计函数 1.=SUMPRODUCT(array1,[array2]...) ​ 返回对应的区域或数组的乘积之和. 默认运算是乘法,但加.减和除也可能. 2.=COUNT 计数 3.= ...

  3. MySQL安装与连接

    1.安装 下载地址:https://dev.mysql.com/downloads/mysql/ 常见问题及解决办法:https://blog.csdn.net/chen97_08/article/d ...

  4. Windows10下安装numpy

    1.https://bootstrap.pypa.io/get-pip.py 下载get-pip.py(右键另存为即可) 2.命令行下在get-pip.py所在文件夹下运行get-pip.py 3.命 ...

  5. 探索grafana

    因为zabbix的监控图形不够美观,功能也不够强大, 那么就用到了grafana 填写zabbix插件配置: 如下需要根据主机群组和主机名来完成图形: grafana报警如下: 解决如下: 更改标准设 ...

  6. 插入排序——C语言

    插入排序 插入排序(Insertion-Sort)的算法描述是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.  (每步将一个待 ...

  7. 初始STM32

    主要内容: 1.什么是STM32 STM32有什么 STM32怎么选型号 一:什么是STM32 ST— 意法半寻体,是一个公司名,即SOC厂商(ARM是IP厂商,STM32中内核由ARM设计,外设例如 ...

  8. Eureka常见问题

    一 Eureka注册慢问题默认情况下,服务注册到Eureka Server过程较慢.在开发或测试时,常常希望加速这一过程,从而提高工作效率.服务注册涉及到周期性心跳,默认30秒一次.只有当实例.服务端 ...

  9. ajax回调函数,全局变量赋值后,ajax外无法获取的解决

    1 ajax回调函数内,function的执行与ajax外是异步的,常导致全局变量赋值后,再次使用此变量人无法获取. 所以,可以把需要的步骤,独立放在functuon中,在ajax回调函数中执行.可较 ...

  10. 怎样确保页面中的js代码一定是在DOM结构生成之后再调用

    有这样一类问题, 如下所示, 就是在dom结构没有生成时就在js代码中调用了, 此时就会报错: <head> <script> console.log(document.bod ...