农夫在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. caffe 入门实例3 fine-turning

    占坑,使用fine-turning初始化参数...

  2. 03_主线程联网问题&ANR&子线程不能修改UI

    如果不使用HAXM,恐怕网页源码查看器无法获取servlet的源码.初步猜测是安卓模拟器运行速度太慢了.如果CPU不支持VT-x的话,HAXM是安装不上的.所以可以先开启VT-x. 可以参考几篇文章h ...

  3. js 回调函数

    一.前奏 在谈回调函数之前,先看下下面两段代码: 不妨猜测一下代码的结果. function say (value) { alert(value);}alert(say);alert(say('hi ...

  4. 瞎比比系列---1st

    A - 项目管理HDU4858 /* 题意: 这个项目有n个节点, 两个节点间可能有多条边,不过一条边的两端必然是不同的节点. 0的时候:接下来两个数u v表示给项目u的能量值加上v: 1的时候: 这 ...

  5. poj2239 poj1274【二分匹配】

    题意: 就是尽可能的选多的课 思路: 把课程和上课的时间看作二分图 跑一跑二分匹配就好了 #include<iostream> #include<cstdio> #includ ...

  6. 手机测试用例-wap测试用例

    Software Test Case P/F comment tester test time P/F comment tester ID 功能描述 操作步骤 预期结果 备注 wap_001 wap ...

  7. layui开始时间小于结束时间

    直接上代码 // var endDate= laydate.render({ // elem: '#end_time',//选择器结束时间 // type: 'datetime', // min:&q ...

  8. Hexo瞎折腾系列(7) - Coding Pages申请SSL/TLS证书错误

    问题 今天我的个人站点SSL/TLS证书到期,我的证书是由Coding Pages提供的,每次申请成功后有效期是三个月,证书到期后可以继续免费申请.但是当我登陆进入Coding Pages服务的后台并 ...

  9. tcp聊天交互

    #****setver端 import socket sk = socket.socket() adress = ('127.0.0.1', 8032) sk.bind(adress) sk.list ...

  10. JavaScript入门2

    5.document对象:Document对象是window对象的一个对象属性,代表浏览器窗口中装载的整个HTML文档.文档中的每个HTML元素对应着JavaScript对象. 因为document代 ...