***参考Catch That Cow(BFS)
Catch That Cow
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 67 Accepted Submission(s) : 22
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?
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int maxn=; int vis[maxn+];
int n,k; struct node
{
int x,c;
}; int BFS()
{
queue<node> q;
while(!q.empty())
q.pop();
memset(vis,,sizeof(vis));
node cur,next;
cur.x=n,cur.c=;
vis[cur.x]=;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
for(int i=; i<; i++)
{
if(i==)
next.x=cur.x-;
else if(i==)
next.x=cur.x+;
else
next.x=cur.x*;
next.c=cur.c+;
if(next.x==k)
return next.c;
if(next.x>= && next.x<=maxn && !vis[next.x])
{
vis[next.x]=;
q.push(next);
}
}
}
return ;
} int main()
{ freopen("1.txt","r",stdin); while(~scanf("%d%d",&n,&k))
{
if(n>=k)
{
printf("%d\n",n-k);
continue;
}
printf("%d\n",BFS());
}
return ;
}
***参考Catch That Cow(BFS)的更多相关文章
- 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)
Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...
- 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+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- POJ - 3278 Catch That Cow BFS求线性双向最短路径
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
随机推荐
- mac安装lavaral
1,安装composer # brew install josegonzalez/php/composer
- Redis同步操作失败的原因
今天弄了下 Redis 的主从同步,设置方法其实很简单的,但崩溃的是遇到个莫名其妙的问题,始终同步不了.. 看了看错误日志: Unable to connect to MASTER: Invalid ...
- ubuntu16.04 禁用Guest用户
.打开终端(快捷键 Ctrl+Alt+T) .编辑50-no-guest.conf文件,按照以下命令编辑, sudo gedit /usr/share/lightdm/lightdm.conf.d/- ...
- CoreAnimation的使用
一.CABasicAnimation CABasicAnimation *anim = [CABasicAnimation animation]; anim.keyPath = @"posi ...
- nyoj 523 双向广搜
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstd ...
- <蛇形填数>--算法竞赛 入门经典(第2版)- 3.1 数组 程序3-3 蛇形填数
蛇形填数: 在n×n方阵里填入1,2,....,n×n,要求填成蛇形.例如,n = 4 时方阵为: 10 11 12 1 9 16 13 2 8 15 14 3 7 ...
- 阅读国外大神对this的分析,自己的总结
大神的分析地址:http://javascriptissexy.com/understand-javascripts-this-with-clarity-and-master-it/#comment- ...
- 学习笔记(C++Primer)--易错点总结(Chapter2)
2.1.2Type Conversions(1/10/2017) 1.If we assign an out-of-range value to an object of unsigned type, ...
- php浏览历史记录的方法
本文实例讲述了php浏览历史记录的方法.分享给大家供大家参考.具体实现方法如下: /** * 商品历史浏览记录 * $data 商品记录信息 */private function _history($ ...
- python 在mongo 中建立索引
import pymongo mongo = pymongo.Connection('localhost') collection = mongo['database']['user'] collec ...