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 ...
随机推荐
- C#中调用unmanaged DLL
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- [java学习笔记]java语言基础概述之函数的定义和使用&函数传值问题
1.函数 1.什么是函数? 定义在类中的具有特定功能的一段独立小程序. 函数也叫做方法 2.函数的格式 修饰符 返回值类型 函数名(参数类型 形式参数1, 参数类型 形式参数2-) { ...
- ASP.NET 发送电子邮件简介
1.补充知识 (1)POP3和SMTP服务器是什么? 简单点来说:POP3 用于接收电子邮件 ,SMTP 用于发送电子邮件. (1)POP3具体指什么? POP3(Post Office Protoc ...
- PHP 归并排序
在我们日常的程序开发时候,有时候需要对一个已知的集合按照一定的规则进行排序,其实当数据的规模不太大时或者数据的有序特征比较明显,其实我们可以采用其它的排序算法例如:Bubble Sort, Inser ...
- maven项目转eclipse工程的命令:eclipse.bat
call mvn clean:clean call mvn eclipse:eclipse -DdownloadSources=true @pause 复制以上内容,保存为eclipse.bat 以后 ...
- http://phantomjs.org/page-automation.html
http://phantomjs.org/page-automation.html install brew curl -LsSf http://github.com/mxcl/homebrew/ta ...
- 【jquery插件】收藏
http://www.oschina.net/project/tag/356/jquery-file-upload
- 强连通分量Tarjan模板
#include<iostream> #include<stdio.h> #include<string.h> #include<stack> #inc ...
- 第二天就跳票 将wikipedia上的英文词条翻译为中文 手动
忙着改简历一整天,刚说完一天一博,就要跳票了. 还是写点东西吧. 今天又翻译了一个维基百科上的条目,刚过一天就忘了怎么弄,还得回头翻帖子.在这先记一下,省的以后找不到. 1.注册个wiki账号,轻松过 ...
- 解决MS Azure 不能ping的问题
PsPing v2.01 PsPing implements Ping functionality, TCP ping, latency and bandwidth measurement. Use ...