POJ 3278:Catch That Cow
| Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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
发现广度优先搜索适合什么题呢?就是那种在每个点都给你几个选择,然后问你最短路径的问题,对,就是这样,这样的题目最适合广度优先搜索。
这次的这个就是,每次Farmer有三个选择,然后求最短到达目的地的行程。简直是广搜的模板题。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int color[1000005];
int dis[1000005]; queue<int> q; int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int N,K;
cin>>N>>K; if(N==K)
{
cout<<0<<endl;
}
else
{
memset(color,0,sizeof(color));
memset(dis,0,sizeof(dis)); q.push(N);
while(!q.empty())
{
N=q.front();
q.pop();
if(N-1==K || N+1==K || 2*N==K)
{
cout<<dis[N]+1<<endl;
break;
} if(color[N-1]==0 && N-1>=0)
{
color[N-1]=1;
dis[N-1]=dis[N]+1;
q.push(N-1);
}
if(color[N+1]==0)
{
color[N+1]=1;
dis[N+1]=dis[N]+1;
q.push(N+1);
}
if(color[2*N]==0 && 2*N<=100000)
{
color[2*N]=1;
dis[2*N]=dis[N]+1;
q.push(2*N);
}
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3278:Catch That Cow的更多相关文章
- POJ 3278: Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 44613 Accepted: 13946 ...
- POJ 3278:The merchant(LCA&DP)
The merchant Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6864 Accepted: 2375 Desc ...
- [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)
BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...
- 抓住那只牛!Catch That Cow POJ-3278 BFS
题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- 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(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
随机推荐
- 非极大抑制睔PYTHON实现
非极大抑制(Non-maximum suppression)python代码实现原创Butertfly 发布于2018-11-20 18:48:57 阅读数 293 收藏展开定位一个物体,最后算法就找 ...
- [Android]如何导入已有的外部数据库
转自:http://www.cnblogs.com/xiaowenji/archive/2011/01/03/1925014.html 我们平时见到的android数据库操作一般都是在程序开始时创建一 ...
- Spark Scheduler 模块(上)
在阅读 Spark 源代码的过程中,发现单步调试并不能很好的帮助理解程序.这样的多线程的分布式系统,更好的阅读源代码的方式是依据模块,分别理解. 在包 org.apache.spark 下面有很多 ...
- SQLlite的olestr
关于SQLite的connection string说明:http://www.connectionstrings.com/sqlite/ SQLite GUI客户端列表:http://www.sql ...
- 07 MySQL常用内置函数
常用函数 1.字符串函数 CONCAT(str1,str2,..,strN) 将str1,str2..strN 拼接成一个字符串,当这里有任何一个为NU ...
- 九九乘法表的四种三角形排布方式(for循环以及while循环的互换)
#region //右上 for (int i = 1; i <= 9; i++){ for (int j = 1; j <= 9; j++){ if (i > j){ Consol ...
- vi/vim常用操作
什么是vim? Vim是从 vi 发展出来的一个文本编辑器.代码补全.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用,和Emacs并列成为类Unix系统用户最喜欢的文本编辑器. vim的 ...
- redisTemplate注入为空
springboot2.*集成redis时,redis工具类中的redisTemplate注入后总是为空. 问题代码还原: 1.工具类定义成静态工具类,@Resource注入redisTemplate ...
- pixi的图片处理
pixi的图片处理 var texture = PIXI.Texture.fromImage('sprite.png');var sprite = new PIXI.Sprite(texture) ...
- 一对多关联按照一方的id查找信息的一个笛卡尔积问题
mapper中,关联的一对多,正确的结果应该是按照一方的id查找,根据映射得到的是一个一方对象,对象里嵌套这list属性,但是结果却出来了多条,在sql中实验 一方中123456789只有一条数据 多 ...