本文来源于:http://blog.csdn.net/svitter

意甲冠军:给你一个数字n, 一个数字k。分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x。求主人找到奶牛的时间(奶牛不移动)

题解:最基础的BFS可是脑子犯抽WA了3遍- =

注意:

1.数组范围1~1<<5

2.visit去重。(BFS最基础的)

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue> using namespace std; bool visit[100010]; struct step
{
int x;
int t;
step(){}
step(int a, int b):x(a), t(b){}
}; inline bool judgeNum(int i)
{
if(i > 100000 || i < 0)
return false;
return true;
} int main()
{
int n, k;
queue <step> que;
step top;
int temp; while(~scanf("%d%d", &n, &k))
{
memset(visit, 0, sizeof(visit));
visit[n] = 1;
que.push(step(n, 0));
while(!que.empty())
{
top = que.front();
if(k == top.x)
{
printf("%d\n", top.t);
while(!que.empty())
{
que.pop();
}
break;
}
temp = top.x+1;
if(judgeNum(temp) && !visit[temp])
{
que.push(step(top.x+1, top.t+1));
visit[temp] = 1;
} temp = top.x-1;
if(judgeNum(temp) && !visit[temp])
{
que.push(step(top.x-1, top.t+1));
visit[temp]= 1;
} temp = top.x*2;
if(judgeNum(temp) && !visit[temp])
{
que.push(step(2*top.x, top.t+1));
visit[temp] = 1;
} que.pop();
} }
return 0;
}

POJ3279 Catch That Cow(BFS)的更多相关文章

  1. HDU 2717 Catch That Cow (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...

  2. POJ 3278 Catch That Cow(bfs)

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

  3. HDU 2717 Catch That Cow(BFS)

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

  4. Catch That Cow(BFS)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. ***参考Catch That Cow(BFS)

    Catch That Cow Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  6. Catch That Cow (bfs)

    Catch That Cow bfs代码 #include<cstdio> #include<cstring> #include<algorithm> #inclu ...

  7. poj 3278(hdu 2717) Catch That Cow(bfs)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. 题解报告:hdu 2717 Catch That Cow(bfs)

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

  9. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...

随机推荐

  1. Qt移动版优化后台云服务、支持跨平台开发

    http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5MDE0Mjc4MA==&appmsgid=10000461&itemidx=2&am ...

  2. MySQL HINT:Straight_JOIN

    来自生产环境的朋友.可能都会碰到:          原本运行良好的查询语句,过了一段时间后,可能会突然变得很糟糕     一个很大可能的原因就是数据分布情况发生了变化     从而导致MySQL优化 ...

  3. ThinkPHP 的模型使用详细介绍--模型的核心(七)

    原文:ThinkPHP 的模型使用详细介绍--模型的核心(七) 注意:本节是ThinkPhp框架对数据操作的核心处理部分 大家还是在这里看清楚可以将其剪切放到代码编辑器中查看 本章节给大家着重介绍模型 ...

  4. NYOJ 914 Yougth的最大化

    Yougth的最大化 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 Yougth如今有n个物品的重量和价值各自是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价 ...

  5. OpenRisc-50-or1200的freeze模块分析

    引言 之前,我们分析or1200的控制通路中的sprs模块和except模块,本小节,我们就分析一下or1200控制通路的最后一个模块,就是freeze模块. 1,整体分析 freeze模块,顾名思义 ...

  6. [Network]Wireless and Mobile

    Wireless 1 Introduction 1.1 Elements 1. Wireless Hosts Wireless does not mean mobility. 2. Base Stat ...

  7. Iterator 和 Iterable 差别和联系

    用Iterator模式实现遍历集合  Iterator模式是用于遍历集合类的标准訪问方法.它能够把訪问逻辑从不同类型的集合类中抽象出来,从而避免向client暴露集合的内部结构. 比如,假设没有使用I ...

  8. RH033读书笔记(17) - Summary

    End of Unit 1 • Questions and Answers • Summary • Open source and the right to modify • The GNU Proj ...

  9. Android 推断当前的界面是否是桌面的方法

    在开发桌面飘浮控件的时候,须要通过service查看当前是不是桌面,从而控制漂浮窗的显现与消失,以下的代码就是推断是否是桌面的方法 /** * 推断当前界面是否是桌面 */ private boole ...

  10. HDU 4704 Sum (费马定理+快速幂)

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Subm ...