题目链接: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. poj1717 Dominoes (背包)

    A domino is a flat, thumbsized tile, the face of which is divided into two squares, each left blank ...

  2. 设置java、maven环境变量(怕麻烦以后直接来这里复制)

    这种方法更为安全,它可以把使用这些环境变量的权限控制到用户级别,如果你需要给某个用户权限使用这些环境变量,你只需要修改其个人用户主目录下的.bash_profile文件就可以了. ·用文本编辑器打开用 ...

  3. 填报志愿(codevs 2930)

    题目描述 Description 高考已经结束,而志愿填报正在进行中- 吴校长的学校里有n位同学,每位同学有ki个愿意去的大学.而在吴老师的省份中,有m所大学有招生名额.根据往年的经验,对于每所大学( ...

  4. CodeForces 303B Rectangle Puzzle II

    题意: 给定一个靠着坐标轴长为n,宽为m的矩形和 矩形中的一个点A,求在这个矩形内部一个 长宽比为a/b的小矩形,使这个小矩形的长宽尽量大使点A在小矩形内部,并且点A尽量靠近小矩形的中心 CF的思维题 ...

  5. 对Netflix Ribbon的Loadbalancer类源码设计合理性的一点质疑

    首先,这只是我个人的一点质疑,可能是因为我自己菜没有领悟到作者的意思,也正因此,想发出来跟大家一起探讨. 在昨晚,我因为在编写自己的开源项目的负载均衡模块(这是我开源项目的介绍:https://www ...

  6. #1045 - Access denied for user 'root'@'localhost' (using password: NO)的问题

    问题描述:  修改了root的密码,然后在http://localhost/phpmyadmin下无法登录了  报错:#1045 - Access denied for user 'root'@'lo ...

  7. [Python] 'unicode' object is not callable

    在Python中,出现'unicode' object is not callable的错误一般是把字符串当做函数使用了.

  8. Android SurfaceView与View

    SurfaceView介绍 SurfaceView是视图(View)的继承类,这个视图里面内嵌了一个专门用于绘制的Surface.你可以控制这个Surface的格式和尺寸,而SurfaceView控制 ...

  9. CentOS配置DHCP服务器

    知识储备 bootp (boot protocol) 早前用于无盘工作站,dhcp的前身 IP初次分配完成,以后固定mac和IP绑定关系 dhcp基础 获取IP步骤 step1: Client dhc ...

  10. 自定义header参数时的命名要求

    HTTP头是可以包含英文字母([A-Za-z]).数字([0-9]).连接号(-)hyphens, 也可义是下划线(_).在使用nginx的时候应该避免使用包含下划线的HTTP头.主要的原因有以下2点 ...