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. 向后移动一个 ...
随机推荐
- android名词
NDK:Native Development Kit JNI:Java Native Interface
- android的文件操作
http://blog.csdn.net/fenghome/article/details/5668598 android的文件操作要有权限: <uses-permission android: ...
- Codeforces Round #554 (Div. 2) 选做
C. Neko does Maths 题意 给 \(a,b\) ,求一个最小的 \(k\) 使得 \(\text{lcm}(a+k,b+k)\) 最小. \(a,b\le 10^9\) 题解 \(\g ...
- SpringMVC之ModelAndView的用法
https://blog.csdn.net/qq30211478/article/details/78016155
- Unity VFX踩坑
先挖个坑,要实现如下效果,这几个Demo都来自Unity日本分布技美总监Keijiro Takahashi,效果过于酷炫,请收下膝盖.. PS:先缓缓,VFX暂时还不支持安卓 Keijiro Taka ...
- jmeter简单压测、下载文件
一.jmeter做简单压测(单机) 1.添加需要压测的HTTP请求 2.添加聚合报告 3.设置压测场景 4.查看聚合报告 二.多机同时进行压测 1.在需要连接的电脑上打开jmeter bin目录下的 ...
- Linux添加虚拟内存 && 修改Linux系统语言
Linux添加虚拟内存 首先执行free -h查看内存状况: total used free shared buff/cache available Mem: 1.8G 570M 76M 8.4M 1 ...
- C# SqlBulkCopy 避免插入重复数据(不重复即插入)
之前写过一篇 C# SqlBulkCopy 大量数据导入到数据库 的文章介绍了大量数据导入到数据库的高效方法. 这篇文章与之有些关联,在这之前所想的是做全量插入,每次run这个job就会清空然后插入, ...
- 第二十一篇 关联管理器(RelatedManager)
关联管理器(RelatedManager) lass RelatedManager "关联管理器"是在一对多或者多对多的关联上下文中使用的管理器.它存在于下面两种情况: Forei ...
- mysql 索引使用教程
1.什么索引 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的位置信息.更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询速度. ...