农夫在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. 收藏产品判断、html 在 UIwebView里面显示

     收藏产品功能 要求:用户点击收藏,如果已经收藏,用户点击就取消收藏 写法一: 点击事件{ if (!isSelect) { [sender setImage:[UIImage imageNamed: ...

  2. Windows下允许redis远程访问

    1.安装很简单,下载安装包安装即可,略过: 下载msi的安装包,会自动安装进服务: 2.远程访问 Redis默认只允许本地访问,要使Redis可以远程访问,需修改配置文件: 我用的redis3.2 , ...

  3. UVaLive 7461 Separating Pebbles (暴力)

    题意:给出平面上的两类点,判断是否能画一条直线将两类点完全分割开来. 析:用暴力去枚举任意两点当作直线即可. 代码如下: #pragma comment(linker, "/STACK:10 ...

  4. 2019年5月22日 AY 程序员调侃语录

    我是AY,杨洋,做wpf开发的,最近得了一种病,程序员患得患失综合征.同事说,我年纪在变大,技术跟不上.业余之间,我原创了写了一些语录,给大家中午休息,累疲惫的时候,开心放松下. 1.活着的每一天都无 ...

  5. VMware安装Centos 7,网络连接问题以及解决方案

    转自: https://www.cnblogs.com/owaowa/p/6123902.html 在这里表示万分感谢 在使用CentOS虚拟机后,出现了无法上网的情况,使用主机ping虚机地址可以p ...

  6. 1049 - One Way Roads 观察 dfs

    http://lightoj.com/volume_showproblem.php?problem=1049 题意是,在一副有向图中,要使得它变成一个首尾相连的图,需要的最小代价. 就是本来是1--& ...

  7. 关于spring mvc 和struts2的描述与对比

    链接:https://www.nowcoder.com/questionTerminal/cf803beb7e3346caa636e4eaa3a8c2e9来源:牛客网 ---------------- ...

  8. PHP生成和获取XML格式数据

    在做数据接口时,我们通常要获取第三方数据接口或者给第三方提供数据接口,而这些数据格式通常是以XML或者JSON格式传输,本文将介绍如何使用PHP生成XML格式数据供第三方调用以及如何获取第三方提供的X ...

  9. DataTable数据导入DataBase

    EXcel---->DataTable--->DataBase /// <summary> /// Excel数据-->DataTable导入DataBase /// & ...

  10. 用NPOI从Excel到DataTable

    NPOI功能强大,不用装Excel,就可以操作表格中数据----Excel.Sheet------>DataTable private IWorkbook workbook = null; pr ...