poj 3278 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 //简单bfs
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cstring> using namespace std;
int visit[];
int n,k; struct node
{
int x,step;
}; void bfs()
{
node st,ed;
queue <node> q;
st.x=n;
st.step=;
memset(visit,,sizeof(visit));
visit[n]=;
q.push(st);
while(!q.empty())
{
st=q.front();
q.pop();
if(st.x==k)
{
cout<<st.step<<endl;
return ;
}
if(st.x+<=&&visit[st.x+]==)
{
visit[st.x+]=;
ed.x=st.x+;
ed.step=st.step+;
q.push(ed);
}
if(st.x->=&&visit[st.x-]==)
{
visit[st.x-]=;
ed.step=st.step+;
ed.x=st.x-;
q.push(ed);
}
if(*st.x<=&&visit[*st.x]==)
{
visit[*st.x]=;
ed.step=st.step+;
ed.x=st.x*;
q.push(ed);
}
}
} int main()
{
while(cin>>n>>k)
{
bfs();
}
return ;
}
poj 3278 Catch That Cow bfs的更多相关文章
- 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: 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 难度: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 ...
随机推荐
- 记JavaScript的入门学习(一)
2016年11月20日算是每天都会利用点时间去思考,去学习,接下来便是我学习JavaScript的前前后后,希望早日学完JavaScript和jQuery 之前学校毕业设计选择了一个如下图的题目 希望 ...
- Openjudge-NOI题库-旅行-数论
题目描述 Description 转眼毕业了,曾经朝夕相处的同学们不得不都各奔东西,大家都去了不同的城市开始新的生活.在各自城市居住了一段时间后,他们都感到了一些厌倦,想去看看其他人的生活究竟如何,于 ...
- Webupload + MVC 之上传下载
最近工作中用到了 MVC 的上传及下载 , 写下感受 本项目中用到的是百度的webuploader <!--引入Jquery--> <script src="~/Conte ...
- 将JSON对象转化为数组对象
package web.helper; import java.util.ArrayList; import net.sf.json.JSONArray; import web.model.Abstr ...
- 设计模式 -- 代理模式 (Proxy Pattern)
定义: 为其他对象提供一种代理以控制对这个对象的访问: 角色: 1,抽象主题类,(接口或者抽象类),抽象真实主题和代理的共有方法(如下Subject类): 2,具体实现的主题类,继承或者实现抽象主题类 ...
- AsyncHttpClient 中的重定向和 setEnableRedirects 方法异常解决
今天使用 AsyncHttpClient 开源库,遇到个很崩溃的问题: 方法 setEnableRedirects(false); 从名称上看应该是重定向开关的方法,设置为 false 后则普通请 ...
- CSU 1810 Reverse
湖南省第十二届大学生计算机程序设计竞赛$H$题 规律,递推. 这种问题一看就有规律.可以按位统计对答案的贡献.即第$1$位对答案作出了多少贡献,第$2$位对答案作出了多少贡献.....累加和就是答案. ...
- 修改LibreOffice Draw中定义的样式名称
目前我使用的是LibreOffice 4.2.4.2.经过以往的测试和使用经验,这是诸多版本中较为稳定和bug相对较少的.今天无意中发现该版本的LibreOffice Draw存在一个问题:样式名称修 ...
- Dynamics CRM 相关资料
links: 1.The Microsoft Dynamics CRM Team Blog 2.申请试用Dynamics CRM 2013 http://www.microsoft.com/zh-cn ...
- jQuery的animate在火狐浏览器上不支持backgroundPositionX的解决方法
在网上找的ffSupp.js文件 /** * 自定义backgroundPosition的animate,支持火狐,jQuery1.8以上版本 * @author Meleong * v1.00 */ ...