博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3278 Catch That Cow(搜索BFS)
阅读量:5016 次
发布时间:2019-06-12

本文共 2472 字,大约阅读时间需要 8 分钟。

                                                          Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27816   Accepted: 8556

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 - 1 or + 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.

Source

解题报告:
这道题其实挺简单的,但是好久没有写搜索题了,很不细心,
#include 
#include
#include
using namespace std; const int N = 1000010; int visit[2 *N], queue[2 * N], step[2 * N], ans;//queue起到队列的作用 void BFS(int begin, int end) {
int rear, front, a, b[3], i; rear = 0;//队列的尾 front = 0;//队列的头 rear ++; queue[rear] = begin;//初始化 while (front < rear) {
front ++; a = queue[front]; if (a == end)//找到 {
ans = step[a]; return ; } else {
b[0] = a + 1; b[1] = a - 1; b[2] = 2 * a; for (i = 0; i <=2; ++i) {
if (b[i] >= 0 && b[i] <= 1000000 && !visit[b[i]])//当时写的时候1000000写成了100000少写了一个零,Runtime Error好几次!! {
visit[b[i]] = 1;//标记 rear ++; queue[rear] = b[i];//插到队尾 step[b[i]] = step[a] + 1;//步数加1 } } } } } int main() {
int begin, end; while (scanf("%d%d", &begin, &end) != EOF) {
memset(visit, 0, sizeof(visit)); memset(queue, 0, sizeof(queue)); memset(step, 0, sizeof(step)); visit[begin] = 1; BFS(begin, end); printf("%d\n", ans); } return 0; }
,哎!这道题的题意:就是在一条数轴上,求起始点到终点的最小步数,很显然得用BFS借助于队列;
代码如下:

转载于:https://www.cnblogs.com/lidaojian/archive/2012/03/01/2376269.html

你可能感兴趣的文章
新Android学习计划
查看>>
搭建Modelsim SE仿真环境-使用do文件仿真
查看>>
iframe有哪些缺点?
查看>>
第76节:Java中的基础知识
查看>>
git工具学习
查看>>
state thread
查看>>
SAP学习笔记
查看>>
[Spark][Python][Application]非交互式运行Spark Application 的例子
查看>>
spring基础之Spring的Bean生命周期(三)
查看>>
最近两年的生活
查看>>
2014-8-5 NOIP(雾)模拟赛
查看>>
css上下垂直居中方法总结
查看>>
BDFZOI bdfz历险记
查看>>
计算器编写过程
查看>>
值得拥有!精心推荐几款超实用的 CSS 开发工具
查看>>
CocoaPods的安装以及使用
查看>>
ACM/ICPC 之 BFS-广搜+队列入门-抓牛(POJ3278)
查看>>
Java 正则表达式
查看>>
分享两个获取指定网站服务器、环境详情的网站
查看>>
Jquery ui datepicker 设置日期范围,如只能隔3天
查看>>