Catch That Cow 经典广搜
链接:http://poj.org/problem?id=3278
题目:
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
N and
K
Output
Sample Input
5 17
Sample Output
4
Hint

#include <cstdio>
#include<queue>
#include<cstring>
#define position 100005
using namespace std;
int n,k;
struct num//定义结构体表示每一个位置
{
int x;//位置坐标
int step;//走到这一步的步数(我们将时间转换为步数理解,每一步需要一分钟)
};
bool visit[position];//标记,判断是否走过
void bfs()
{
queue<num> steps;//定义队列存储位置
num start,now,next;//start=起点,now=前的位置,next=下一步的位置
memset(visit,false,sizeof(visit));//初始化标记(false表示未走过)
start.x=n;//初始化起点
start.step=0;
steps.push(start);//起点入队
visit[start.x]=true;//标记起点(表示走过)
while(!steps.empty())
{
now=steps.front();//取队列首元素表示当前位置
steps.pop();//首元素出列
if(now.x==k)//如果当前位置就是牛的位置(得到结果)输出步数,退出调用函数
{
printf("%d\n",now.step);
return;
}
for(int i=0;i<3;i++)
{
if(i==0)
next.x=now.x+1;
else if(i==1)
next.x=now.x-1;
else if(i==2)
next.x=now.x*2;//对三种方法进行判定
if(next.x>=0&&next.x<position&&!visit[next.x])
{
visit[next.x]=true;//表示走过了
next.step=now.step+1;//步数加一
steps.push(next);//入队
}
}
}
}
int main()
{
scanf("%d %d",&n,&k);
bfs();
return 0;
}
注意范围
Catch That Cow 经典广搜的更多相关文章
- 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 (BFS广搜)
问题描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...
- HDU 2717 Catch That Cow (深搜)
题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants t ...
- poj 3278 Catch That Cow 优化深搜
这题的思想很简单,就是每次找出队列里面花费时间最少的来走下一步,这样当我们找到k点后,所花费的时间一定是最少的. 但要用一个标记数组vis[200010],用来标记是否走过.否则会内存溢出. #inc ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- Catch That Cow(BFS广搜)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
随机推荐
- 正整数a、b、c、d满足ab=cd,则a+b+c+d必定为合数。
正整数a.b.c.d满足ab=cd,则a+b+c+d必定为合数. 证法一:记s=a+b+c+d.如果四个数全为1,s=4,显然是合数.考虑四个数非全1的情形,由对称性,不妨令a>1. 设p是a的 ...
- maven下载出错
求解
- vue 接入 vod-js-sdk-v6.js 完成视频上传
东西有点多,耐心看完.按照操作一步一步来,绝对能成功 首先:npm 引入 npm install vod-js-sdk-v6 mian.js 全局引入 //腾讯云点播 import TcVod f ...
- Django——数据库连接配置
配置settings.py : DATABASES = { 'default': { #default表示默认,也可以指定app 'ENGINE': 'django.db.backends.mysql ...
- Django中使用MySQL数据库的连接配置
1. 安装pymysql pip install pymysql 2. 导入 # 在与 settings.py 同级目录下的 __init__.py 中引入模块和进行配置 import pymysql ...
- 基本ServletWEB项目
项目搭建 项目链接https://gitee.com/zhangjzm/smbms.git 前置知识,Servlet JSP 结构图 搭建maven web项目 1.搭建一个maven web项目 2 ...
- Mysql常用sql语句(5)- as 设置别名
测试必备的Mysql常用sql语句系列 https://www.cnblogs.com/poloyy/category/1683347.html 需要注意,创建数据库和创建表的语句博文都在前面哦 整个 ...
- 性能测试必备命令(1)- free
性能测试必备的 Linux 命令系列,可以看下面链接的文章哦 https://www.cnblogs.com/poloyy/category/1819490.html 介绍 显示系统的内存使用情况 语 ...
- salesforce零基础学习(一百零七)Dynamic Action
说一下项目中常见的甲方的需求.背景如下:Order在SF端生成以后,在status为completed以后,需要点击按钮同步到SAP或者其他的MDM,客户希望的是,如果 order的状态为 compl ...
- ms sql 带自增列 带外键约束 数据导入导出
1,生成建表脚本 选中要导的表,点右键-编写表脚本为-create到 ,生成建表脚本 2,建表(在新库),但不建外键关系 不要选中生成外键的那部分代码,只选择建表的代码 3,导数据,用SQL STU ...