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 ...
随机推荐
- sqoop mysql--->hive 报错 (ERROR tool.ImportTool: Import failed: java.io.IOException: java.lang.ClassNotFoundException: org.apache.hadoop.hive.conf.HiveConf)
ERROR tool.ImportTool: Import failed: java.io.IOException: java.lang.ClassNotFoundException: org.apa ...
- maven 编译替换占位符
首先开启资源配置的插件,由此插件替换占位符 <plugin> <groupId>org.apache.maven.plugins</groupId> <art ...
- Maven profile动态选择配置条件
背景 在开发过程中,我们的软件会面对不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置.以及一些软件运行过程中的基本配置, ...
- nDPI 的论文阅读和机制解析
nDPI: Open-Source High-Speed Deep Packet Inspection Wireless Communications & Mobile Computing C ...
- 框架 Hibernate 2
持久化类百度百科 http://baike.baidu.com/link?url=gNus-8jhK0Mi401aIR-16YUAnWKzOJfeMagUV8_t5iG8235JyjMrmZPd7rI ...
- LINUX下安装pcre出现WARNING: 'aclocal-1.15' is missing on your system错误的解决办法
1.下载安装包 wget https://ftp.gnu.org/gnu/automake/automake-1.15.tar.gz 2.解压 tar -xzvf automake-1.15.tar. ...
- 【星云测试】Wings-让单元测试智能全自动生成
Wings-让单元测试智能全自动生成 前言 单元测试是保证软件质量非常有效的手段,无论是从测试理论早期介入测试的理念来看或是从单元测试不受UI影响可以高速批量验证的特性,所以业界所倡导的测试驱动开发, ...
- mac终端输入python默认打开python3
*** 1. 终端打开.bash_profile文件 ***open ~/.bash_profile *** 2. .bash_profile文件内容 ***# Setting PATH for Py ...
- [iOS]UIInterpolatingMotionEffect重力视觉差
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- List和ArrayList
1.为什么List list = new ArrayList()? 也不是非常夸张的说,一定要用List代替ArrayList接收,只是说这样是良好的编码习惯,便于以后代码可能重构. 首先要明白接口和 ...