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
Hint
题意:FJ要抓奶牛,输入n(FJ的位置),k(奶牛的位置),求FJ抓到奶牛所需最少时间。
FJ有三种走法:
1:向前移动一步,花费一分钟
2:向后退后一步,花费一分钟
3:向前移动当前位置的2倍,花费一分钟
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int MAX=;
bool vis[MAX];
int step[MAX];
queue <int >q;
int bfs(int n,int k)
{
int head,next;
q.push(n);
step[n]=;
vis[n]=true;
while(!q.empty())
{
head=q.front(); //取队首
q.pop();
for(int i=; i<; i++)
{
if(i==)
next=head-;
if(i==)
next=head+;
if(i==)
next=head*;
if(next<||next>=MAX) //排除出界情况
continue;
if(!vis[next])
{
q.push(next); //入队
step[next]=step[head]+;//步数加一
vis[next]=true; //标记访问
}
if(next==k)
return step[next];
} }
}
int main()
{
int n,k;
while(cin>>n>>k)
{
memset(vis,false,sizeof(vis));
memset(step,,sizeof(step));
while(!q.empty())
q.pop();
if(n>=k)
cout<<n-k<<endl;
else
cout<<bfs(n,k)<<endl;
}
return ;
}
poj 3278 Catch That Cow(bfs+队列)的更多相关文章
- 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: 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(基础水)
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 ...
随机推荐
- SparseArray
使用SparseArray更加节省内存空间的使用,SparseArray也是以key和value对数据进行保存的.使用的时候只需要指定value的类型即可.并且key不需要封装成对象类型. Has ...
- linux-ubuntu 安装配置Redis
>wget http://download.redis.io/releases/redis-3.2.6.tar.gz #下载redis源码包 >tar -zxvf redis-3.2.6. ...
- sql:inner join,left join,right join,full join用法及区别
join的语法: select [字段] from [表名1] inner/left/right/full join [表名2] on [表名1.字段1] <关系运算符> [表名2.字段2 ...
- 第一个android ijkplayer播放器
创建一个ijkplayer的播放器项目,需要三步设置: 一.在activity_main.xml中添加播放器标签 <com.smallart.myapplication.media.IjkVid ...
- 20172306《Java程序设计》第四周学习总结
20172306 <Java程序设计>第四周学习总结 教材学习内容总结 第四章: 1. 类和对象的回顾:除了看书,我还上网找了一下两者的一些区别. 2. 编写类时,了解到初始化.形式参数. ...
- LibreOJ #6013. 「网络流 24 题」负载平衡 最小费用最大流 供应平衡问题
#6013. 「网络流 24 题」负载平衡 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- Zookeeper 修改heap size
对应原文出处: https://support.pivotal.io/hc/en-us/articles/201861286-Zookeeper-service-heapsize-is-10GB-or ...
- NC 5系查询引擎做报表
在集团下打开查询引擎管理节点,选中查询设计,鼠标移动到创建,点击文件夹 文件夹名字按需求起,创好文件夹后选中该文件夹后鼠标移动到创建,点击对象. 按需求起好编码和名称 都创建好后,点击SQL手工设计 ...
- eclipse中tomcat调试正确关联源码
1.build path中jar包关联本地源码 2.tomcat中添加source关联工程lib下的jar包 以上两步即可. 可解决tomcat直接关联本地源码debug时无法计算表达式的情况. 错误 ...
- 查看PHP代码执行的时间
$t1 = microtime(true); //...要执行的代码$t2 = microtime(true); echo '耗时'.round($t2-$t1,3).'秒';