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

Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 124528   Accepted: 38768

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.

题意:给你两个点 N,K,你有三种走法:X+1 ,X-1,X*2 ,求从N到K走的最少步数。

代码附有Wrong Answer 和 Runtime Error的几个参考原因。

 #include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std; /**
* 造成Runtime Error的原因有两点:
* 1.数组开小了,也就是下面的maxn可能只开到 1e5稍多一点,这是不对的,在搜索过程中有个 2*x ,这会导致数组大小应该大于1e5的两倍。
* 2.在判断!mark[x-1]的时候,没有x>0这一个约束条件,如果x = 0 ,则 x-1会导致数组越界;
* 3.在 x+1 和 2*x这两种情况下,应该限制 x<=K;
*/
/**
* 造成Wrong Answer 的原因之一:在判断 x-1 的情况的时候,不要有 x<=K
*/
const int maxn = 2e5+;
bool mark[maxn];
int N,K;
struct node
{
int x;
int step;
}; //队列的写法;
void bfs()
{
queue<node> q;
struct node cu,ne;
cu.x = N;
cu.step = ;
mark[N] = ;
q.push(cu);
while(!q.empty())
{
cu = q.front();
q.pop();
if(cu.x == K)
{
printf("%d\n",cu.step);
return ;
}
//下面的三种情况应该是并列关系,不应该满足一种情况就不判断别情况了
//我开始写成了if-else if-else if形式,想成不是并列的形式了;
//如果实在找不到哪里错了,可以打印cu.x变量,根据变量的值找问题;
if(cu.x>&&!mark[cu.x-])
{
ne.x = cu.x - ;
ne.step = cu.step + ;
mark[ne.x] = ;
q.push(ne);
}
if(cu.x<=K&&!mark[cu.x+])
{
ne.x = cu.x + ;
ne.step = cu.step + ;
mark[ne.x] = ;
q.push(ne);
}
if(cu.x<=K&&!mark[cu.x*])
{
ne.x = *cu.x;
ne.step = cu.step + ;
mark[ne.x] = ;
q.push(ne);
}
}
}
int main()
{
while(scanf("%d%d",&N,&K)!=EOF)
{
memset(mark,,sizeof(mark));
bfs();
}
return ;
}

Ac代码

POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)的更多相关文章

  1. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

  2. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

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

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

  4. POJ 3278 Catch That Cow(简单BFS)

    题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次 ...

  5. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  6. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  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+队列+剪枝]

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

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

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

随机推荐

  1. Vue 子组件调用父组件 $emit

    <!DOCTYPE html><html>    <head>        <meta charset="utf-8">      ...

  2. linux mysql access denied for user ‘root’@’localhost'(using password:YES)

    linux安装完mysql后,使用程序连接报以上错误解决方法,重新设置密码,步骤如下 1.先停掉原来的服务 service mysqld stop 2.使用安全模式登陆,跳过密码验证 mysqld_s ...

  3. CentOS查看卸载openjdk

    1.查看openjdk版本 java -versionjava version "1.7.0_51" OpenJDK Runtime Environment (rhel-2.4.5 ...

  4. 小程序的get和post需要注意的地方

    在进行异步请求之前先去小程序后台配置好服务器域名. get请求:header: { 'content-type':'json'}就可以. post请求:header: { 'content-type' ...

  5. ios开发者较为好用的工具

    移动应用世界发生了巨大的变化,无论是在风格上还是在市场竞争上,消费者意识都推动了移动应用开发公司的崛起. 新的应用以及新的功能的出现 Apple IOS是为用户提供最新工具和升级的平台之一,它为iPh ...

  6. 面试官问你JS基本类型时他想知道什么?

    面试的时候我们经常会被问答js的数据类型.大部分情况我们会这样回答包括:1.基本类型(值类型或者原始类型): Number.Boolean.String.NULL.Undefined以及ES6的Sym ...

  7. Python编程小坑

    在Pycharm中编辑文本文件,如果使用\r\n会产生两个空行? exit("xxx),会导致Process finished with exit code 1,所以如果要输入信息然后退出, ...

  8. C#中的抽象方法,虚方法,接口之间的对比

    1.首先来看一看抽象类 抽象类是特殊的类,不能够被实例化:具有类的其他特性:抽象方法只能声明于抽象类中,且不包含任何实现 (就是不能有方法体),派生类也就是子类必须对其进行重写.另外,抽象类可以派生自 ...

  9. uva 156 - Ananagrams (反片语)

    csdn:https://blog.csdn.net/su_cicada/article/details/86710107 例题5-4 反片语(Ananagrams,Uva 156) 输入一些单词,找 ...

  10. Go Web 使用工具

    前端工具: sublime text3 下载:https://www.sublimetext.com/3 中文版设置:https://jingyan.baidu.com/article/9c69d48 ...