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 ...
随机推荐
- PHP面试系列 之Linux(三)---- Vi/Vim编辑器
vi 是 unix 家族下最功能强大的文字编辑器,而 vim 則是 vi 的加强版, 编辑模式 指令 說明 * i 在游標位置進入編輯模式 I 在游標行的第一個非空白字元進入編輯模式 * a ...
- PAT——1019. 数字黑洞
给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第1个数字减第2个数字,将得到一个新的数字.一直重复这样做,我们很快会停在有“数字黑洞”之称的6174 ...
- 【最新最全】为 iOS 和 Android 的真机和模拟器编译 Luajit 库
编译 Luajit 库,的确是一个挑战.因为官网的教程,在当前版本的 Xcode 和 NDK 环境中,已经不适用了.以前只是编译了适用于真机的 Luajit 库.最近在尝试编译模拟器 Luajit 库 ...
- py基础__socket编程
目录 Python基础__socket编程 1.计算机网络 2.socke编程 3.socketserver模块 4.思考 Python基础__socket编程 1.计算机网络 1.OSI网络七层模型 ...
- mysql数据库的导入导出命令
1.备份一个数据库 mysqldump -h localhosot -uroot -ppassword 数据库名>数据库所在路径(路径+数据库名.sql) 注意:导出的数据库文件.sql文件不能 ...
- php的mysqli_connect函数显示 No such file or directory错误以及localhost换成127.0.0.1执行成功
Centos7环境-php7-MariaDB5.5.60 (新安装的php7,执行php -m 显示有mysqli模块,php.ini没有改其它) 测试代码为: <?php //~ echo d ...
- ssm多数据源的操作
公司要求,需要使用两个数据库,一个mysql,一个oracle.所以需要配置两个数据库来进行操作. 1.首先,需要在jdbc.properties文件中将两个库的配置数据写入,不过一个写driver, ...
- html-头标签的使用
HTML两部分组成 head和body ** 在head里面的标签就是头标签 ** title标签:表示在标签上显示的内容 ** meta标签:设置页面的一些相关内容(用的比较少) <meta ...
- Windows.Devices API in a C# WinForm Win32 Desktop application in Windows 10
https://social.msdn.microsoft.com/Forums/windows/en-US/40b4f86d-b6d5-430e-a235-7fc003dec4c4/how-to-u ...
- Python使用代理的方法
我们在做爬虫的过程中经常会遇到这样的情况:最初爬虫正常运行,正常抓取数据,一切看起来都是那么的美好,然而一杯茶的功夫可能就会出现错误,比如403 Forbidden:出现这样的原因往往是网站采取了一些 ...