题目传送门

 /*
BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstring>
using namespace std; const int MAXN = 1e6 + ;
const int INF = 0x3f3f3f3f;
int n, m;
int d[MAXN];
bool vis[MAXN]; void BFS(void)
{
memset (vis, , sizeof (vis));
for (int i=; i<=1e6; ++i) d[MAXN] = ; queue<int> q;
q.push (n); d[n] = ; vis[n] = true; while (!q.empty ())
{
int x = q.front (); q.pop ();
if (x == m) break; int xl = x - ; int xr = x + ; int x2 = x * ; if (xl >= && !vis[xl])
{
q.push (xl); d[xl] = d[x] + ;
vis[xl] = true;
}
if (xr <= 1e6 && !vis[xr])
{
q.push (xr); d[xr] = d[x] + ;
vis[xr] = true;
}
if (x2 <= 1e6 && !vis[x2])
{
q.push (x2); d[x2] = d[x] + ;
vis[x2] = true;
}
} } int main(void) //POJ 3278 Catch That Cow
{
//freopen ("POJ_3278.in", "r", stdin); while (scanf ("%d%d", &n, &m) == )
{
BFS ();
printf ("%d\n", d[m]);
} return ;
}

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

  1. 搜索 || BFS || POJ 3278 Catch That Cow

    农夫在x位置,下一秒可以到x-1, x+1, 2x,问最少多少步可以到k *解法:最少步数bfs 要注意的细节蛮多的,写在注释里了 #include <iostream> #include ...

  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 (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

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

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

  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. 完善dedecms站内搜索代码,为搜索结果添加第*页

    自那些平凡而伟大的程序猿开发了内容管理系统(cms),为了让看客们更快地找到自己感兴趣的内容,他们不断完善站内搜索代码,形成了一个小型的站内搜索引擎.可能有些网站模板设计师没考虑到seo的问题,很多站 ...

  2. Nginx 开启PATHINFO支持ThinkPHP框架实例

    ThinkPHP支持通过PATHINFO和URL rewrite的方式来提供友好的URL,只需要在配置文件中设置 'URL_MODEL' => 2 即可.在Apache下只需要开启mod_rew ...

  3. Python序列切片的注意事项

    a=[1,2,3,4,5,6,7,8,9,10] 1)普通切片,形如array[m:n],只包含起始索引m,和不被包含在结果内的终点索引n, 注意终点索引可以大于序列的大小(长度),若终点索引大于序列 ...

  4. 在Shell里面判断字符串是否为空

     在Shell里面判断字符串是否为空 分类: Linux shell2011-12-28 23:18 15371人阅读 评论(0) 收藏 举报 shell 主要有以下几种方法: echo “$str” ...

  5. Android中获取IMSI和IMEI

    TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); Str ...

  6. spring事物传播属性

    PROPAGATION_REQUIRED Support a current transaction; create a new one if none exists.  支持一个当前事务;如果不存在 ...

  7. Valid Perfect Square

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  8. Java,Calendar 获得明天凌晨的时间time

    /** * 获得明天凌晨的时间time * * @return */ private long getNextDayZeroTime() { Calendar calendar = Calendar. ...

  9. 修改PHP网站默认首页

    一般默认的首页文件是index.php index.php3 index.html index.htm之类的,要想修改为myNewIndex.php, 进入服务器Apache目录,找到httpd.co ...

  10. 学习BigDecimal用法

    一.简介 Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算.双精度浮点型变量double可以处理16位有效数.在实际应用中,需要对更大或者更 ...