POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 88732 | Accepted: 27795 |
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?
Input
Output
Sample Input
- 5 17
Sample Output
- 4
Hint
Source
- #include<iostream>
- #include <algorithm>
- #include <stdio.h>
- #include <string.h>
- #include<queue>
- #define MAX 100001
- using namespace std;
- queue<int> q;//使用前需定义一个queue变量,且定义时已经初始化
- bool visit[MAX];//访问空间
- int step[MAX]; //记录步数的数组不能少
- bool bound(int num)//定义边界函数
- {
- if(num<||num>)
- return true;
- return false;
- }
- int BFS(int st,int end)
- {
- queue<int> q;//使用前需定义一个queue变量,且定义时已经初始化
- int t,temp;
- q.push(st);//进队列
- visit[st]=true;
- while(!q.empty())//重复使用时,用这个初始化
- {
- t=q.front();//得到队首的值
- q.pop();//出队列,弹出队列的第一个元素,并不会返回元素的值
- for(int i=;i<;++i) //三个方向搜索
- {
- if(i==)
- temp=t+;
- else if(i==)
- temp=t-;
- else
- temp=t*;
- if(bound(temp)) //越界
- continue;
- if(!visit[temp])//访问空间未被标记
- {
- step[temp]=step[t]+;
- if(temp==end)
- return step[temp];
- visit[temp]=true;//标记此点
- q.push(temp);//将temp元素接到队列的末端;
- }
- }
- }
- }
- int main()
- {
- int st,end;
- while(scanf("%d%d",&st,&end)!=EOF)
- {
- memset(visit,false,sizeof(visit));//visit数组进行初始化操作
- if(st>=end)
- cout<<st-end<<endl;
- else
- cout<<BFS(st,end)<<endl;
- }
- return ;
- }
POJ 3278 Catch That Cow(BFS,板子题)的更多相关文章
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- POJ - 3278 Catch That Cow BFS求线性双向最短路径
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- poj 3278 Catch That Cow bfs
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- poj 3278 Catch That Cow(bfs+队列)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- POJ 3278 Catch That Cow bfs 难度:1
http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...
- POJ - 3278 Catch That Cow bfs 线性
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
随机推荐
- 【java】System成员输入输出功能out、in、err
package System输入输出; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOu ...
- iOS 获取当前应用的信息以及用户信息:版本号手机号手机型号
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; CFShow(infoDictionary); // ap ...
- APP安全--网络传输安全 AES/RSA/ECC/MD5/SHA
移动端App安全如果按CS结构来划分的话,主要涉及客户端本身数据安全,Client到Server网络传输的安全,客户端本身安全又包括代码安全和数据存储安全.所以当我们谈论App安全问题的时候一般来说在 ...
- xml生成方式二(Xml序列化器XmlSerializer)
一.andoirdAPI提供了xml生成和解析的API: XmlSerializer xs = Xml.newSerializer();和XmlPullParser xmlPullParser = X ...
- bzoj 1566: [NOI2009]管道取珠
Description Input 第一行包含两个整数n, m,分别表示上下两个管道中球的数目. 第二行为一个AB字符串,长度为n,表示上管道中从左到右球的类型.其中A表示浅色球,B表示深色球. ...
- badboy 录制脚本并并发脚本
很久没有研究过接口相关的工具了,一个偶然的机会听说了 badboy,可以录制jemter脚本, 查了资料 还可以并发,于是乎,实践才知道. http://www.badboy.com.au/ 官网,我 ...
- python3.5 + PyQt5 +Eric6 实现的一个计算器
目前可以实现简单的计算.计算前请重置,设计的时候默认数字是0,学了半天就做出来个这么个结果,bug不少. python3.5 + PyQt5 +Eric6 在windows7 32位系统可以完美运行 ...
- 百度图表插件echart简单应用,简单配置一些要显示的样式及种类
从echart官网下载js,然后引入jq即可运行.足够简单应用了 关键词:echart控制:图标标题.数据标题.折线图.柱状图切换按钮.恢复刷新图表按钮.保存为图片按钮.坐标系控制.坐标数据.坐标倾斜 ...
- [编织消息框架][网络IO模型]NIO(select and poll)
上面测试论证系统内核在read data时会阻塞,如果我们在把第一个阶段解决掉那么性能就会提高 NIO 编程 JDK 1.4中的java.nio.*包中引入新的Java I/O库,其目的是提高速度.实 ...
- Vue 子组件无法使用 $emit 向父组件传参
问题描述: