POJ 3278 抓奶牛(BFS入门题)
描述
农夫约翰已被告知逃亡牛的位置,并希望立即抓住她。他开始于一个点Ñ(0≤ Ñ ≤100,000)上的数线和牛是在点ķ(0≤ ķ上相同数目的线≤100,000)。农夫约翰有两种交通方式:步行和传送。
*行走:FJ可以在一分钟内从任意点X移动到X -1或X + 1 点
*传送:FJ可以在一分钟内从任意点X移动到点2× X .
如果母牛不知道它的追求,根本不动,那么农夫约翰需要多长时间才能找回它?
输入
产量
样本输入
5 17
样本输出
4
暗示
#include <iostream>
#include <cstring>
#include <queue>
using namespace std; const int maxn=;
int s,e;
int d[maxn]; int bfs()
{
queue<int> que;
memset(d, 0x3f, sizeof(d));
d[s]=;
que.push(s); while(que.size())
{
int t=que.front(); que.pop();
int dx[]={,-,t};
if(t==e)
break;
for(int i=; i<; i++)
{
int nx=t+dx[i];
if(nx>= && nx<maxn && d[nx]==0x3f3f3f3f)
{
que.push(nx);
d[nx]=d[t]+;
}
}
}
return d[e];
} int main()
{
cin>>s>>e;
if(s>=e)
cout<<s-e<<endl;
else
cout<<bfs()<<endl;
return ;
}
记住bfs函数如果有返回值,最好写成单一出口的那种,
不然编译器可能会提示:
用gcc编译一个程序的时候出现这样的警告:
warning: control reaches end of non-void function
它的意思是:控制到达非void函数的结尾。就是说你的一些本应带有返回值的函数到达结尾后可能并没有返回任何值。这时候,最好检查一下是否每个控制流都会有返回值。
POJ 3278 抓奶牛(BFS入门题)的更多相关文章
- poj 3254 状压dp入门题
1.poj 3254 Corn Fields 状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- POJ 3126 Prime Path bfs, 水题 难度:0
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- POJ P2318 TOYS与POJ P1269 Intersecting Lines——计算几何入门题两道
rt,计算几何入门: TOYS Calculate the number of toys that land in each bin of a partitioned toy box. Mom and ...
- Bad Hair Day POJ - 3250 (单调栈入门题)
Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-cons ...
- POJ 2001 字典树(入门题)
#include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #i ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- 【BFS】POJ 3278
POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最 ...
随机推荐
- js方法的积累
对字符串编码解码编码 encodeURIComponet(str); 解码 decodeURIComponet(str); 及时反应的方法,监听input值是否发生改变$('#username') ...
- jstl 与 el表达式
jar下载地址参考:https://blog.csdn.net/qq_30062589/article/details/80224080 <%@ page language="java ...
- 判断是手机请求还是pc请求
网址 http://detectmobilebrowsers.com/ string u = Request.ServerVariables["HTTP_USER_AGENT"]; ...
- AutoBat
var currentDir = Directory.GetCurrentDirectory(); var filePath = string.Format("{0}\\Auto.bat&q ...
- Lint found fatal errors while assembling a release target
1.Android 打包错误信息 Generate signed Bundle or APK 打包时,报了一个错,错误信息如下: Error:Execution failed for task ´: ...
- textarea 标签的使用
<textarea rows="行数" cols="列数">默认出现文本</textarea> 阻止拉伸:style="res ...
- MySQL Workbench在archlinux中出现 Could not store password: The name org.freedesktop.secrets was not provided by any .service files的错误
MySQL Workbench在archlinux中出现 Could not store password: The name org.freedesktop.secrets was not prov ...
- [iOS] 测试设备解决自签名证书问题
不多说,解决过程都是泪. 用了最简单粗暴的方式. 1. 将你的自签名证书,放到测试设备可以访问的站点上 2. 用safari访问上面的地址,直接将证书安装到本设备上 搞掂! Have fun with ...
- day09 python之函数进阶
楔子 假如有一个函数,实现返回两个数中的较大值: def my_max(x,y): m = x if x>y else y return mbigger = my_max(10,20)print ...
- 第五篇——Struts2的默认Action
默认Action 1.当访问action不存在时,可以通过制定默认action的方式避免出现错误代码页面: 2.使用default-action-ref 指定默认 action. 项目实例 1.项目结 ...