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 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<stdio.h>
#include<queue>
using namespace std; struct Node{
int x,y;
}node;
int b[]; int main()
{
int n,k;
queue<Node> q;
scanf("%d%d",&n,&k);
if(n==k) printf("0\n"); //特判
else{
node.x=n;
node.y=;
q.push(node);
b[n]=;
while(q.size()){
node.x=q.front().x*;
node.y=q.front().y+;
if(node.x<=&&node.x>=){
if(node.x==k){
printf("%d\n",node.y);
break;
}
if(b[node.x]==){
b[node.x]=;
q.push(node);
}
}
node.x=q.front().x+;
node.y=q.front().y+;
if(node.x<=&&node.x>=){
if(node.x==k){
printf("%d\n",node.y);
break;
}
if(b[node.x]==){
b[node.x]=;
q.push(node);
}
}
node.x=q.front().x-;
node.y=q.front().y+;
if(node.x<=&&node.x>=){
if(node.x==k){
printf("%d\n",node.y);
break;
}
if(b[node.x]==){
b[node.x]=;
q.push(node);
}
}
q.pop();
}
}
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+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- 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: 61826 Accepted: 19329 ...
- POJ - 3278 Catch That Cow bfs 线性
#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...
- 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 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
随机推荐
- Django1.11.4中文文档
Django管理站点¶ 自动管理界面是Django最强大的部分之一.它从您的模型中读取元数据,以提供一个快速,以模型为中心的界面,让受信任的用户可以管理您网站上的内容.管理员建议的使用仅限于组织的内部 ...
- ComboBox在WPF中的绑定示例:绑定项、集合、转换,及其源代码
示例1.Selector(基类) 的示例Controls/SelectionControl/SelectorDemo.xaml <Page x:Class="Windows10.Con ...
- RabbitMQ 内存和磁盘警告
内存和磁盘警告 有两种情况下,Rabbit MQ会停止读取客户端的连接socket,为了防止crash崩溃,这两种情况是: 内存使用达到配置值 磁盘空间下降到配置值下 在这两种情况下,服务器会临时阻塞 ...
- 基于注解的Sping AOP详解
一.创建基础业务 package com.kang.sping.aop.service; import org.springframework.stereotype.Service; //使用注解@S ...
- Angular中的内置指令和自定义指令
NG中的指令,到底是什么(what)? 为什么会有(why)?以及怎样使用(how)? What: 在NG中,指令扩展HTML功能,为 DOM 元素调用方法.定义行为绑定数据等. Why: 最大程度减 ...
- 配置tomcat,访问端口改为80
首先:找到tomcat的的config文件夹下的server.xml文件: 编辑server.xml 保存server.xml文件,重启tomcat服务器,即可. 亲测好使.
- Linux集群基础
Linux集群基础 作者:Danbo 时间:2015-7-12 集群概述 什么是集群?集群是一组协同工作的服务器实体.用以提供比单一服务实体更具扩展性和可用性的平台. 集群的分类 1.HPC(High ...
- linux命令行快捷键记录
摘自: http://www.cnblogs.com/webzhangnan/p/3221410.html [移动光标] Ctrl+A 标移到行首.它在多数文本编辑器和 Mozilla 的 URL 字 ...
- AndroidManifest配置之uses-sdk
uses-sdk配置 uses-sdk用来设置app对android系统的兼容性.它包含三个可选的配置项,分别为android:minSdkVersion,android:targetSdkVersi ...
- YII的RBAC
转自:http://www.cppblog.com/guojingjia2006/archive/2013/01/15/197298.html 开始准备 Yii提供了强大的配置机制和很多现成的类库.在 ...