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 ...
随机推荐
- python执行linux和window的命令
linux: python执行shell脚本常用的方法 import os val=os.system("shell语句") >>> val=os.system ...
- PAT——1041. 考试座位号
每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...
- DataFrame概念与创建
一 概念 Pandas是一个开源的Python数据分析库.Pandas把结构化数据分为了三类: Series,1维序列,可视作为没有column名的.只有一个column的DataFrame: Dat ...
- 一次傻叉的安装ubuntu虚拟机记录
因为最近要做一些开发工作,涉及到游戏前后端到的开发,在将代码上传到github之前,所有开发版本的源文件都保存在我的虚拟机svn仓库.所以,就装了两台虚拟机,一台用作svn仓库以及git同步与更新,另 ...
- linux文件系统写过程简析
linux写入磁盘过程经历VFS -> 页缓存(page cache) -> 具体的文件系统(ext2/3/4.XFS.ReiserFS等) -> Block IO ->设备 ...
- SpringMVC找不到对应的页面
确认springmvc配置文件视图解析器配置正确. <!-- 视图解析器 --> <bean class="org.springframework.web.servlet. ...
- JOB SERVER 负载均衡
JOB SERVER 负载均衡 一.体系结构 1.job server group job server group 是由一个或者多个job server 组成的,做为一个整体对外提供服务,在内部实现 ...
- iOS项目启动及启动时间优化
app的启动入口Main函数: int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc ...
- 在Swift中使用AutoLayout-VFL(AutoLayout-VFL笔记)
1.背景 iOS开发这几年, UI布局工具从frame到Masonry到SnapKit, sb和xib的AutoLayout也用过, 但是代码版本的AutoLayout倒是没用过, 最近一年, 频频发 ...
- javascript 异步操作,串形执行,并行执行
单线程模型 单线程模型指的是,JavaScript 只在一个线程上运行.也就是说,JavaScript 同时只能执行一个任务,其他任务都必须在后面排队等待. 注意,JavaScript 只在一个线程上 ...