题目
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. ftp-server(对象存储)

    1.背景 在腾讯云弄了一个对象存储,想通过ftp上传照片 说明连接: 腾讯云:https://cloud.tencent.com/document/product/436/7214 GitHub:ht ...

  2. (二)SpringBoot之springboot开发工具的使用以及springboot插件的功能

    一.springboot开发工具的使用 1.1 在项目中添加springoot开发工具 1.2 功能 修改代码后点击保存自动重启 二.springboot插件的功能 2.1 maven配置 <p ...

  3. oracle查询十分钟之前的数据

    select * from TABLE as of timestamp sysdate - 10/1440 t WHERE ColName='1111'; TABLE:表名 WHERE:查询子句 sy ...

  4. Ubuntu 14.04 用户如何安装深度音乐播放器和百度音乐插件

    播放本地音乐或者收听国外的音乐电台,Ubuntu 14.04 自带的音乐播放器 Rhythmbox 完全能够满足,但是如果你想有像酷狗那样的国内播放器就需要折腾一下,还好有深度音乐播放器,这是一款完全 ...

  5. Mysql 中删除重复数据(保留一条)

    sql去重 先根据需要去重的字段进行分组,取到主键值最小的记录(id 是主键,删除重复的 record_id 的记录) select min(id) from tb_traffic_wf_record ...

  6. g++ 生成C++ .so库文件,并调用示例

    Tags: g++ C++ so library   在Linux系统下用g++命令编译C++程序.也可以生成so,a链接库   示例一 编译时链接so库 Test.h 文件内容   Main.cpp ...

  7. 字符串转json数组

    import net.sf.json.JSONArray;import net.sf.json.JSONObject; String content = inBankOdd.getContent(). ...

  8. 批量删除checkbox前台后台

    <%@ page contentType="text/html;charset=UTF-8" %><%@ include file="/WEB-INF/ ...

  9. python matplotlib以日期为x轴作图

    from datetime import datetime, date, timedelta import matplotlib.pyplot as plt import tushare as ts ...

  10. linux:# vi /etc/profile -bash: vi: command not found 的解决办法

    /bin/vi /etc/profile 直接用全路径vi,linux下一切皆文件,进去把profile文件内容改一下,一定是profile出了问题 export JAVA_HOME=/usr/jav ...