Catch That Cow(BFS广搜)
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
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
struct point
{
int x;///记录位置
int count;///记录步数
};
queue<point>q;
struct point s,now,t;
int vis[];///假设FJ开始的位置就是100000,那么变化两倍之后就是200000
int bfs(int n,int m)
{
int j;
while(!q.empty())
{
q.pop();
}///清空队列
memset(vis,,sizeof(vis));
vis[s.x]=;
q.push(s);
while(!q.empty())
{
t=q.front();
if(t.x==m)
return t.count;
for(j=; j<; j++)
{
now=t;
if(j==)
{
now.x=now.x+;
}
else if (j==)
{
now.x=now.x-;
}
else if(j==)
{
now.x=now.x*;
}
now.count++;
if(now.x==m)
{
return now.count;
}
if(now.x>=&&now.x<=&&vis[now.x]==)
{
vis[now.x]=;
q.push(now);
}
}
q.pop();
}
return ;///二者开始的位置相同
}
int main()
{
int n,m,ans;
while(scanf("%d%d",&n,&m)!=EOF)
{
s.x=n;
s.count=;
ans=bfs(n,m);
printf("%d\n",ans);
}
return ;
}
反思:这道题和之前的那一道剑客救公主那一道题一样,不仅仅需要考虑题意之中的搜索方式,还要考虑搜索不到或者起始位置与终止位置相同等特殊情况,该去如何设置被调函数,该去返回一个什么样的值,这两道题都是因为这一点使我wa了好多次,引以为戒。
Catch That Cow(BFS广搜)的更多相关文章
- Catch That Cow (BFS广搜)
问题描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...
- Catch That Cow(广搜)
个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John h ...
- poj 3278 Catch That Cow (广搜,简单)
题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...
- HDU2717 Catch That Cow 【广搜】
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Catch That Cow 经典广搜
链接:http://poj.org/problem?id=3278 题目: Farmer John has been informed of the location of a fugitive co ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- hdu 1195:Open the Lock(暴力BFS广搜)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 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,最先 ...
- BFS广搜题目(转载)
BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
随机推荐
- Git 远程推送被拒绝的一种解决方案
今天在推送的时候发生了如下错误信息: error: 无法推送一些引用到 'https://gitee.com/von_w/demo_app.git'提示:更新被拒绝,因为您当前分支的最新提交落后于其对 ...
- Python豆瓣源
pip install -i https://pypi.doubanio.com/simple/ xxxx
- Linux中Zookeeper部署和集群部署
自己网上下载安装包,我下载的是tar.gz安装包直接解压,也可以下载rpm格式 1.下载zookeeper安装包,放到/usr/local/zookeeper安装包网上下载 2.解压文件tar -zx ...
- PHP7.1中使用openssl替换mcrypt
PHP7.1中使用openssl替换mcrypt 在php开发中,使用mcrypt相关函数可以很方便地进行AES加.解密操作,但是PHP7.1中废弃了mcrypt扩展,所以必需寻找另一种实现.在迁移手 ...
- yii学习笔记(3),自定义全局工具函数
在平时开发是经常需要打印数据来调试 常见的打印方式有print_r和var_dump,但是这样打印出来格式很乱不好浏览 在打印函数前后加上<pre></pre>就可以将内容原样 ...
- PAT A1060 (Advanced Level) Practice
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered ...
- python七类之整型布尔值
整型与布尔值 一.关键字:整型 --->int 布尔值----->bool : True 真 False 假 1.整形和布尔值都是不可变得不可迭代的数据类型 2.整型: 主 ...
- 简单R语言爬虫
R爬虫实验 R爬虫实验 PeRl 简单的R语言爬虫实验,因为比较懒,在处理javascript翻页上用了取巧的办法. 主要用到的网页相关的R包是: {rvest}. 其余的R包都是常用包. libra ...
- BZOJ:2763-[JLOI2011]飞行路线(最短路分层图)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2763 解题心得: 第一次见到分层最短路.其实题中说选择k条路径免费,那怎么选k条路径并没 ...
- C#调用c++类的导出函数
C# 需要调用C++东西,但是有不想做成COM,就只好先导出类中的函数处理. 不能直接调用,需单独导出函数 参考:http://blog.csdn.net/cartzhang/article/deta ...