POJ3278——Catch That Cow(BFS)
Catch That Cow
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
Line 1: Two space-separated integers: N and K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4
Hint
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
题目大意:
有个农夫在N点,有个牛在K点。农夫有三种移动方式:1)向前一步 2)向后一步 3)从当所在位置X瞬间移动到2*X点
输出最少进行多少步,农夫能找到牛。(大坑是农夫和牛的活动范围在[0,100000])
解题思路:
bfs即可。注意农夫的可活动范围
ps:HUD 2717与这个题一样,但是是多组输入输出,请注意!
Code:
#include<string>
#include<cstring>
#include<cstdio>
#include<queue>
#include<iostream>
using namespace std;
int dis[],vis[],N,K;
queue<int> q;
int bfs(int N,int K)
{
q.push(N);
dis[N]=,vis[N]=;
while (!q.empty())
{
int x[],i;
int front=q.front();
q.pop();
x[]=front-,x[]=front+,x[]=front*;
for (i=; i<=; i++)
{
if (x[i]>=&&x[i]<=&&!vis[x[i]])
{
q.push(x[i]),vis[x[i]]=,dis[x[i]]=dis[front]+;
if (x[i]==K) return dis[x[i]];
}
}
}
}
int main()
{
cin>>N>>K;
if (N==K) cout<<;
else
cout<<bfs(N,K);
return ;
}
POJ3278——Catch That Cow(BFS)的更多相关文章
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- POJ3278 Catch That Cow(BFS)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- poj3278 Catch That Cow(简单的一维bfs)
http://poj.org/problem?id=3278 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 73973 Accepted: 23308 ...
- 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: 88732 Accepted: 27795 ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
随机推荐
- Paying for upgrades, by Bob Arnson
Following content is reprinted from here, please go to the original website for more information. Au ...
- ZigBee NV层使用
原文转载于http://www.cnblogs.com/yqh2007/archive/2011/05/31/2065284.html 系统NV区:初始化nv数据项 osal_nv_item_in ...
- SQL语句基本概念
星期五 2014年11月21日 SQL语句: sql语句用于检索维护数据库中的数据 [ create alter Select update ...
- DB Cache Reloaded Fix缓存不能被激活解决方法
1.创建wp-content/plugins/db-cache-reloaded-fix/cache目录. 2.将cache权限改为777. 3.拷贝wp-content/plugins/db-cac ...
- DTcms 扩展字段标签调用
前台模版: 文章列表:{dr[author]} 文章内容{model.fields[author]} 点击数 后台CS文件:model.fields["author"].ToStr ...
- syntaxhighlighter语法高亮
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- c# 判断点是否在区域内 点在区域内 在多边形内 判断
方法一 算法 : public int isLeft(Point P0, Point P1,Point P2) { int abc= ((P1.X - P0.X) ...
- leetcode学习笔记--开篇
1 LeetCode是什么? LeetCode是一个在线的编程测试平台,国内也有类似的Online Judge平台.程序开发人员可以通过在线刷题,提高对于算法和数据结构的理解能力,夯实自己的编程基础. ...
- Mac 启用http-dav功能(WebDAV服务器)
启用Mac的WebDAV可以实现文件的上传以及Windows.Linux和Mac之间的数据互传. 客户端使用:windows下使用网上邻居 --> 添加一个网上邻居 --> 输入uplo ...
- Java中的IO流系统详解
Java 流在处理上分为字符流和字节流.字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符.字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组. Java 内用 U ...