农夫在x位置,下一秒可以到x-1, x+1, 2x,问最少多少步可以到k
*解法:最少步数bfs
要注意的细节蛮多的,写在注释里了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
queue<int> q;
int vis[], arr[];
void go(int n, int k)
{
q.push(n);
vis[n] = ;
int flag = ;
while(!q.empty())
{
if(flag) break;
int head = q.front(); q.pop();
int direct[] = {, -, head};
if(head == k) return;
for(int i = ; i < ; i++)
{
int next = head + direct[i];
if(next >= && next <= && !vis[next])//坐标一共有1e5但是可以移动到2x 所以next<=2e5;然后next可能小于0,vis[next]直接RE,所以把vis[next]放在最后,先判next>= 0
{
q.push(next);
vis[next] = ;
arr[next] = arr[head] + ;
}
if(next == k) flag = ;
}
}
return;
}
int main()
{
int n, k;
while(scanf("%d %d", &n, &k) != EOF)
{
while(!q.empty()) q.pop();
memset(vis, , sizeof(vis));
memset(arr, , sizeof(arr));
go(n, k);
printf("%d\n", arr[k]);
}
return ;
}

搜索 || BFS || 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: 46715   Accepted: 14673 ...

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

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

  6. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  7. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  8. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  9. 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. Spring MVC访问页面直接显示源码

    转自:https://blog.csdn.net/u011781521/article/details/78751253

  2. Django中的Ajax详解

    AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Javascript和XML”.即使用Javascript语言与服务器进行异步交互,传输的数据为XML(当 ...

  3. 安装GitLab出现ruby_block[supervise_redis_sleep] action run

    在卸载gitlab然后再次安装执行sudo gitlab-ctl reconfigure的时候往往会出现:ruby_block[supervise_redis_sleep] action run,会一 ...

  4. (水题)洛谷 - P1003 - 铺地毯

    https://www.luogu.org/problemnew/show/P1003 一开始觉得是用树套树来区间修改单点查询,但是发现空间不够开. 看了题解发现这个是静态的问题,而且只问一个点的结果 ...

  5. poj1564

    dfs判重 #include<stdio.h> #include<iostream> #include<cstdio> #include<queue> ...

  6. (九)SpringBoot整合redis框架

    二:添加Redis依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  7. PHP 遍历一个文件夹下所有文件和子文件夹的方法

    话不多说,直接上代码 <?php function my_dir($dir) { $files = []; if(@$handle = opendir($dir)) { while(($file ...

  8. iOS 上传的图片在HTML上显示时,图片方向信息(EXIF Orientation)异常

    将iPhone 6s拍摄的照片上传到服务器之后, 在Web网页上看到图片被逆时针旋转了90度, 这让我很惆怅呐! 出现这个问题其实是因为上传的图片为.jpg格式,.jpg文件含有EXIF信息, 其中E ...

  9. req.getParameter()无法获取参数(附前端json序列化)

    问题:前端用Ajax的post方式想servlet传递参数,servlet的getParameter()方法无法获取参数. 前端代码: $.ajax({ url: '/TestWeb/addBook' ...

  10. JSP九大内置对象的作用和用法总结【转】

    JSP九大内置对象的作用和用法总结?     JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.c ...