题目链接:http://poj.org/problem?id=3278

Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 97563   Accepted: 30638

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

题解:

一开始以为是数学找规律,但是看到数据范围很小,n<=1e5,可以用数组存;而且题目要求的是“最少步数”,而“最少步数”经常都是用BFS求的。再将BFS的思想带入看看,发现可以解决问题。

在第一次提交时,RUNTIME ERROR了。但是再看看代码,数组没有开小,不是数组的问题。后来发现再判断某个位置是否vis时,先判断了是否vis,然后再判断这个位置是否合法,这样会导致数组溢出,所以问题就出现在这里了,需谨慎!!

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5+; int vis[MAXN]; struct node
{
int val, step;
}; queue<node>que;
int bfs(int n, int k)
{
ms(vis,);
while(!que.empty()) que.pop(); node now, tmp;
now.val = n;
now.step = ;
vis[n] = ;
que.push(now); while(!que.empty())
{
now = que.front();
que.pop(); if(now.val==k)
return now.step; tmp.step = now.step+;
if(now.val+>= && now.val+<=1e5 && !vis[now.val+] ) //先判断范围再判断vis !!!
vis[now.val+] = , tmp.val = now.val+, que.push(tmp);
if(now.val->= && now.val-<=1e5 && !vis[now.val-] )
vis[now.val-] = , tmp.val = now.val-, que.push(tmp);
if(now.val*>= && now.val*<=1e5 && !vis[now.val*] )
vis[now.val*] = , tmp.val = now.val*, que.push(tmp);
}
} int main()
{
int n, k;
scanf("%d%d",&n, &k);
cout<< bfs(n,k) <<endl;
}

POJ3278 Catch That Cow —— BFS的更多相关文章

  1. POJ3278——Catch That Cow(BFS)

    Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...

  2. POJ3278 Catch That Cow(BFS)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  3. 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,最先 ...

  4. poj3278 Catch That Cow(简单的一维bfs)

    http://poj.org/problem?id=3278                                                                       ...

  5. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  6. poj3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 73973   Accepted: 23308 ...

  7. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

  8. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  9. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

随机推荐

  1. Virtual Box 安装过程(卸载Vmware后)

    VirtualBox安装前的操作:(或许某些操作不一定有用,但是我是这么做下来的,最后也安装成功了) 步骤一:停止之前安装的vmware的所有服务(如果之前没有安装过虚拟机软件,无需做此操作)VMwa ...

  2. poj 2318 向量的叉积二分查找

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9350   Accepted: 4451 Description ...

  3. 作业调度方案(codevs 1156)

    题目描述 Description 我们现在要利用m台机器加工n个工件,每个工件都有m道工序,每道工序都在不同的指定的机器上完成.每个工件的每道工序都有指定的加工时间. 每个工件的每个工序称为一个操作, ...

  4. linux 抓取访问量排行

    需求: 分析图片服务日志,把日志(每个图片访问次数*图片大小的总和)排行,取top10,也就是计算每个url的总访问大小 语句: awk '{a[$1]+=$10;}END{for(i in a){p ...

  5. 533. Lonely Pixel II

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  6. T1008 选数 codevs

    http://codevs.cn/problem/1008/ 题目描述 Description 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整 ...

  7. stm32f103c8t6命名

    stm32f103c8t6和stm32f103rbt c8:48脚.64k :rb:64脚.128k.

  8. [Servlet&amp;JSP] 标准标签

    在JSP的规范中提供了一些标准标签(Standard Tag),全部的容器都支持这些标签,它能够协助编写JSP时降低Scriptlet的使用. 全部的标准标签都使用jsp:作为前置.这些标准标签是在J ...

  9. SolidEdge 工程图中如何给零件着色 给装配体着色

    点击着色按钮,然后点击更新视图即可.

  10. weex 项目开发(四)项目框架搭建 及 自定义 TabBar 组件

    1.安装  路由模块  及  状态管理模块 npm install vue-router --save npm install vuex --save 2.自定义  TabBar  组件 src / ...