注:本人英语很渣,题目大意大多来自百度~=0=
题目大意
农民约翰需要抓住他的牛,他和他的牛在一条直线上(估计是一维生物),约翰在N (0 ≤ N ≤ 100,000)处,他的牛在 K (0 ≤ K ≤ 100,000) ,约翰下次可以从x移动到x+1或者x-1或者2*x的地方,问约翰最少需要多少步才能找到他的牛。
 
用BFS可以解决~ 不过需要剪枝 不然会MLE 
值得一提的一点是步数不要用结构体保存   或许是我剪枝不够好  用结构体保存会MLE
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <cmath>
#include <cctype>
#define N 200010
using namespace std; bool maps[N];//用来判断此点约翰有没有走过
int a, b, step[N];//a, b 为分别为N, K, //step[i]用来保存约翰走到i点时用了几步 (注 : 不要用结构体保存步数, 不然有可能MLE) void BFS()
{
int q1 = a;
maps[a] = true;
queue<int>q;
q.push(q1); while(!q.empty()) {
int q2 = q.front();
q.pop();
if(q2 == b) {//找到之后输出走了几步
printf("%d\n", step[q2]);
return ;
}
//约翰向走 -1 只有此时他的坐标为正数才会走
int x = q2 - ;
if(!maps[x] && q2 > ) {
maps[x] = true;
q1 = x;
step[q1] = step[q2] + ;
q.push(q1);
}
//由于约翰向后走只能 -1 所以只在q2 < b时才向前走
if(q2 < b){
x = q2 + ;
if(!maps[x]) {
maps[x] = true;
q1 = x;
step[q1] = step[q2] + ;
q.push(q1);
}
x = q2 * ;
if(!maps[x]) {
maps[x] = true;
q1 = x;
step[q1] = step[q2] + ;
q.push(q1);
}
} }
} int main()
{
while(~scanf("%d %d", &a, &b)){
memset(maps, , sizeof(maps));
BFS();
}
return ;
}

POJ 3278 Catch That Cow的更多相关文章

  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 (附有Runtime Error和Wrong Answer的常见原因)

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

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

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

  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 ...

  10. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

随机推荐

  1. HTML 5 应用程序缓存(上)

    什么是应用程序缓存(Application Cache)?HTML5 引入了应用程序缓存,这意味着 web 应用可进行缓存,并可在没有因特网连接时进行访问. 应用程序缓存为应用带来三个优势: 离线浏览 ...

  2. Android混淆打包

    一.理论知识   ProGuard是一款免费的Java类文件压缩器.优化器和混淆器.它能发现并删除无用类.字段(field).方法和属性值(attribute).它也能优化字节码并删除无用的指令.最后 ...

  3. java(2)之前往对象村

    这次说一说面向对象与面向过程的区别以及面向对象的优点.

  4. IOS8解决获取位置坐标信息出错(Error Domain=kCLErrorDomain Code=0)(转)

    标题:IOS8解决获取位置坐标信息出错(Error Domain=kCLErrorDomain Code=0) 前几天解决了在ios8上无法使用地址位置服务的问题,最近在模拟器上调试发现获取位置坐标信 ...

  5. cygwin安装

    我安装的是cygwin2.5.2,相关下载:https://cygwin.com/setup-x86_64.exe 先安装cygwin,x86_64版本,安装时选择库(gcc-core.gcc-c++ ...

  6. Codeigniter文件上传类型不匹配错误

    Codeigniter的文件上传类方便了我们使用PHP来处理文件上传的操作,使用起来非常简单,如下:   $config['upload_path'] = './uploads/'; $config[ ...

  7. python 中的 try/except/else/finally语句

    1.python中try/except/else/finally正常的语句是这样的: try: normal excute block except A: Except A handle except ...

  8. Example For maven-compiler-plugin

    1. Compiling Sources Using A Different JDK The compilerVersion parameter can be used to specify the ...

  9. 1005. Spell It Right (20)

    Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...

  10. c# 邮件发送功能

    //统一由一个邮箱发送录用通知 string strfrom = "";//发件人邮箱地址 string strpow = "";//邮箱密码 string s ...