POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)
题目链接:http://poj.org/problem?id=3278
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 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?
Input
Output
Sample Input
5 17
Sample Output
4
Hint
题意:给你两个点 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的常见原因)的更多相关文章
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- POJ 3278 Catch That Cow(简单BFS)
题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
随机推荐
- uva 10369 Arctic Network (最小生成树加丁点变形)
The Department of National Defence(DND)wishestoconnectseveral northern outposts by a wireless networ ...
- SimpleDateFormat 的 format 方法使用具体解释
Java中怎么才干把日期转换成想要的格式呢.或把字符串转换成一定格式的日期,如把数据库中的日期或时间转换成自己想要的格式,JAVA中提供了SimpleDateFormat类能够实现,下面是Simple ...
- jdk 下载地址 服务器
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
- 从对集合数据去重到Distinct源码分析
今天在写代码的时候要对数据进行去重,正打算使用Distinct方法的时候,发现这个用了这么久的东西,竟然不知道它是怎么实现的,于是就有了这篇文章. 使用的.net core2.0 1.需求 假如我们有 ...
- zookeeper报错 JAVA_HOME is not set
很多开发者安装zookeeper的时候,应该会发现到这么一个问题: JAVA_HOME is not set 好的!那么这个是什么意思呢? 就是说你的 JAVA_HOME 变量没有设定 为什么会提示 ...
- Vue 源码分析——构造函数原型
在执行 npm run dev 的时候 根据script/config.js 文件中的配置 'web-full-dev': { entry: resolve('web/entry-runtime-wi ...
- BZOJ 3053: The Closest M Points(K-D Tree)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1235 Solved: 418[Submit][Status][Discuss] Descripti ...
- tp框架如何处理mysql先排序在分组
$giModel = M('GroupIntegral'); $gi_table = $giModel->order('id desc')->limit('999')->buildS ...
- mysql截取字段并插入到新的字段中
例如:在产品表product表中字段content值为["10"],然后在产品表中新建一个字段product_id,提出字段content的值10,如何实现呢? 解: update ...
- 大数据时代的结构化存储--HBase
迄今,相信大家肯定听说过 HBase,但是对于 HBase 的了解可能仅仅是它是 Hadoop 生态圈重要的一员,是一个大数据相关的数据库技术. 今天我带你们一起领略一下 HBase 体系架构,看看它 ...