bfs—Catch That Cow—poj3278
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 87152 | Accepted: 27344 |
http://poj.org/problem?id=3278
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<iostream>
#include<queue>
#include<cstring>
using namespace std; const int MAX=;
int pace[MAX+];
int main()
{
int n,k;
while(cin>>n>>k)
{
memset(pace,,sizeof(pace));
int t;
queue<int> qu;
qu.push(n);
while(!qu.empty())
{
t=qu.front();
if(t==k)
{
cout<<pace[t]<<endl;
return ;
}
qu.pop();
if(t<MAX&&!pace[t+])
{
qu.push(t+);
pace[t+]=pace[t]+;
}
if(t>&&!pace[t-])
{
qu.push(t-);
pace[t-]=pace[t]+;
}
if(t*<=MAX&&!pace[t*])
{
qu.push(t*);
pace[t*]=pace[t]+;
}
}
}
return ;
}
bfs—Catch That Cow—poj3278的更多相关文章
- 抓住那只牛!Catch That Cow POJ-3278 BFS
题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...
- POJ 3287 (基础BFS) Catch That Cow
这是做的第一道BFS,很基础很简单的题目 广度优先搜索算法如下:(用QUEUE)(1) 把初始节点S0放入Open表中:(2) 如果Open表为空,则问题无解,失败退出:(3) 把Open表的第一个节 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- 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 ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- poj3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 73973 Accepted: 23308 ...
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
随机推荐
- 2017蓝桥杯最大公共子串(C++B组)
题目: 最大公共子串长度问题就是:求两个串的所有子串中能够匹配上的最大长度是多少.比如:"abcdkkk" 和 "baabcdadabc",可以找到的最长的公共 ...
- 31 Exception 异常处理
/* * Exception in thread "main" java.lang.ArithmeticException: / by zero at com.itheima_01 ...
- 2020-3-3 20175110王礼博 《网络对抗技术》Exp1 PC平台逆向破解
目录 1.实践目标与基础知识 2.直接修改程序机器指令,改变程序执行流程 3.通过构造输入参数,造成BOF攻击,改变程序执行流 4.注入Shellcode并执行 5.实验收获与感想 6.什么是漏洞?漏 ...
- matplotlib Transform
2020-04-09 15:09:02 -- Edit by yangray Transform 类是TransformNode的子类,它是所有执行变换的TransformNode的实例的基类.所有非 ...
- 一个不错的intellj 相关的博客
http://my.oschina.net/lujianing/blog?catalog=3300430
- Ant安装与配置
1. 到apache 官网去下载最新版本的ant,http://ant.apache.org/:下载后直接解压缩到电脑上,不需要安装: 2.环境变量配置: 2.1 ->计算机右键->属性- ...
- 【Java】InnerClass 内部类
Java 允许一个类的定义位于另一个类的内部,前者为内部类,后者称为外部类 InnerClass一般定义它的类或语句块之中外部类引用时必须给出完整的名称 成员内部类 & 局部内部类 - 静态成 ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十一)之Holding Your Objects
To solve the general programming problem, you need to create any number of objects, anytime, anywher ...
- 挑战全网最幽默的Vuex系列教程:第二讲 Vuex旗下的State和Getter
先说两句 上一讲 「Vuex 到底是个什么鬼」,已经完美诠释了 Vuex 的牛逼技能之所在(纯属自嗨).如果把 Vuex 比喻成农药里面的刘备,那就相当于你现在已经知道了刘备他是一个会打枪的力量型英雄 ...
- App 开发中判断 ios 和 andriod 常用方法便于修复在两类机型样式不一样等缺陷
判断安卓, ios