POJ3278 Catch That Cow(BFS)
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
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 220000
int n,k;
int vis[maxn]; struct Node
{
int x, time; }node[maxn]; void bfs()
{
queue<Node> Q;
Node r,t,f;
r.time = ; r.x = n;
vis[n] = ;
Q.push(r);
while(!Q.empty())
{
f = Q.front();
Q.pop();
if(f.x == k)
{
printf("%d\n", f.time);
return;
}
if(f.x < k&&!vis[f.x*])
{
t.x = f.x*;
vis[t.x] = ;
t.time = f.time+;
Q.push(t);
}
if(f.x+<=k && !vis[f.x+])
{
t.x = f.x+;
vis[t.x] = ;
t.time = f.time+;
Q.push(t);
}
if(f.x->= && !vis[f.x-])
{
t.x = f.x-;
vis[t.x] = ;
t.time = f.time+;
Q.push(t);
}
}
}
int main()
{
while(~scanf("%d%d", &n, &k))
{
if(k <= n)
{
printf("%d\n", n-k);
continue;
}
memset(vis, , sizeof vis);
bfs();
}
return ;
}
POJ3278 Catch That Cow(BFS)的更多相关文章
- POJ3278——Catch That Cow(BFS)
Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- poj3278 Catch That Cow(简单的一维bfs)
http://poj.org/problem?id=3278 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 73973 Accepted: 23308 ...
- 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,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
随机推荐
- 解决java压缩图片透明背景变黑色的问题
public static BufferedImage resize(int faceWidth,BufferedImage srcImg,HttpServletRequest request) th ...
- eclipse中使用loadrunner java api步骤
1.使用Eclipse新建一个Java工程,名字任意 2.将"%LoadRunner_Home%\classes\lrapi"目录拷贝到工程中 3.将工程导出为Jar包,譬如:命名 ...
- sql加强练习
1.用一条SQL语句 查询出每门课都大于80分的学生姓名 name kecheng fenshu 张三 语文 81张三 数学 75李四 语文 76李四 数学 90王五 语文 81王五 数学 100王五 ...
- springMVC之annotation优化
1.在之前配置的spring配置文件中会有这样的代码: <!-- 方法映射 --> <bean class="org.springframework.web.servle ...
- DB2查询当前时间与指定时间的时间差(相隔的秒数)
DB2查询当前时间与指定时间的时间差(相隔的秒数). 例子:“拍品表 auct_item”中有个“结束时间 end_date”的字段,求结束时间与当前时间的间隔秒数. select (DAYS(a. ...
- 学习selenium所须要具备的技术
学习selenium所须要具备的知识或技术 1.selenium进行的自己主动化測试是基于ui层面的,所以html,css,javascript基本上是不可缺少的,至于javascript,有非常多的 ...
- single-row function和muti-row function
1.single-row function 指一行数据输入,返回一个值的函数. 常见的有 字符函数(如:substr) 日期函数(如:months_between) 数字函数(如:MOD) 转换函数( ...
- python-文件压缩和解压
import tarfile #压缩 tar = tarfile.open('your.tar','w') tar.add('ooo.xml',arcname='ooo.xml') tar.close ...
- 2 读取solr下的索引文件(lucene文件)
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnaly ...
- 1、solr 查询
solr查询参数: q 查询的关键字,此参数最为重要,例如,q=id:1,默认为q=*:*, fl 指定返回哪些字段,用逗号或空格分隔,注意:字段区分大小写,例如,fl= id,title,sor ...