Catch That Cow (BFS)
题目:
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?InputLine 1: Two space-separated integers: N and KOutputLine 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,后退1,当前位置乘以2三种方式到达K处,牛在K处不动,求人从N处到牛的K处所经过的最少步数; 分析:
求最小路径,用BFS;
分为+1,-1,*2三种情况;
分为N在K之前或者在K上的情况和N在K之后的情况,N在K之前就只能通过-1的操作到达K
定义一个数组用于标记经过的位置坐标;
定义一个数组用于记录到达当前位置所需要的最少步数;
在三种情况下,满足范围即入队列,对当前的坐标进行标记,步数加1; AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int Max=;
int n,k;
int f[Max];
int step[Max];
bool cmp(int x)
{
return (x>=&&x<=Max);
}
int a,b;
void bfs()
{
queue<int>s;
s.push(n);
f[n]=;
step[n]=;
while (!s.empty())
{
a=s.front();
s.pop();
if (a==k)
break;
b=a-;
if (cmp(b)&&!f[b])
{ s.push(b);
f[b]=;
step[b]=step[a]+;
}
b=a+;
if (cmp(b)&&!f[b])
{
s.push(b);
f[b]=;
step[b]=step[a]+;
}
b=a*;
if (cmp(b)&&!f[b])
{
s.push(b);
f[b]=;
step[b]=step[a]+;
} }
}
int main()
{ while (scanf("%d %d",&n,&k)==)
{
memset(f,,sizeof(f));
if (n>=k)
printf("%d\n",n-k);
else
{
bfs() ;
printf("%d\n",step[k]);
}
} return ;
}
Catch That Cow (BFS)的更多相关文章
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- Catch That Cow(BFS)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- ***参考Catch That Cow(BFS)
Catch That Cow Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- Catch That Cow (bfs)
Catch That Cow bfs代码 #include<cstdio> #include<cstring> #include<algorithm> #inclu ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)
Catch That Cow Descriptions: Farmer John has been informed of the location of a fugitive cow and wan ...
- poj 3278(hdu 2717) Catch That Cow(bfs)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- POJ3279 Catch That Cow(BFS)
本文来源于:http://blog.csdn.net/svitter 意甲冠军:给你一个数字n, 一个数字k.分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x.求主人找到 ...
随机推荐
- tensorflow deeplabv3 训练自己的数据集
https://blog.csdn.net/malvas/article/details/90776327
- Tensorflow学习教程------实现lenet并且进行二分类
#coding:utf-8 import tensorflow as tf import os def read_and_decode(filename): #根据文件名生成一个队列 filename ...
- textField 总结
/* 通知使用,可以通过接受系统通知来做一些事情 UITextField派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用.除了UIControl类的标准事件,你还可 ...
- 五、Shell脚本高级编程实战第五部
一.条件表达式 在bash的各种流程控制结构中通常要进行各种测试,然后根据测试结果执行不同的操作.有时也和if结合,让我们方便判断. test: 1)判断文件是否存在:test -f file 2) ...
- 嵌入式c语言编码规范
学习嵌入式的同学应该首先掌握嵌入式编码规范,这样才能更好的嵌入式系统. 下面就从这几个方面讲解一下嵌入式c编码规范. 注释风格.排版风格.头文件风格.变量定义.宏定义.函数 1 注释风格 1.1 注 ...
- linux下别名的设定
命令别名设定功能: (alias)假如我需要知道这个目录底下的所有文件 (包含隐藏档) 及所有的文件属性,那么我就必须要下达『 ls -al 』这样的指令串,比较麻烦,我们可以为其设定别名为lm al ...
- Opencv笔记(二):图像的基本操作——续写
1.图像的透视变换 对于视角变换,我们需要一个 3x3 变换矩阵.在变换前后直线还是直线.要构建这个变换矩阵,你需要在输入图像上找 4 个点,以及他们在输出图像上对应的位置.这四个点中的任意三个都不能 ...
- vue中axios的post请求使用form表单格式发送数据
vue使用插件qs实现 (qs 是一个增加了一些安全性的查询字符串解析和序列化字符串的库.) 在jquery中的ajax的方法已将此封装,所以不需要再次序列化 1. 安装 在项目中使用命令行工具输 ...
- Perl语言入门:第九章 使用正则表达式处理文本 示例程序和代码
#! /usr/bin/perl use strict; use warnings; print "\n----------------------------------_substitu ...
- mac用python读取文件常见问题(未完成)
python读取文件常见问题(mac版) 让python的默认编码,和文件的编码保持一致