本文来源于: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. 【Demo 0011】多媒体播放器

    本章学习要点:       1.  掌握AVAudioPlayer 基本使用;       2.  掌握AVPlayer 基本使用;       3.  掌握系统声音播放以及震动;       4. ...

  2. MySQL :: MySQL 5.0 Reference Manual :: 14.4 The MEMORY (HEAP) Storage Engine

    MySQL :: MySQL 5.0 Reference Manual :: 14.4 The MEMORY (HEAP) Storage Engine The MEMORY (HEAP) Stora ...

  3. 【IUML】支持向量机SVM

    从1995年Vapnik等人提出一种机器学习的新方法支持向量机(SVM)之后,支持向量机成为继人工神经网络之后又一研究热点,国内外研究都很多.支持向量机方法是建立在统计学习理论的VC维理论和结构风险最 ...

  4. Android入门第六篇之ListView (一)

    本文来自http://blog.csdn.net/hellogv/ ListView是一个经经常使用到的控件,ListView里面的每一个子项Item能够使一个字符串,也能够是一个组合控件.先说说Li ...

  5. uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)

    题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...

  6. ThinkPhp学习05

    原文:ThinkPhp学习05 一.ThinkPHP 3 的CURD介绍  (了解)二.ThinkPHP 3 读取数据    (重点) 对数据的读取 Read $m=new Model('User') ...

  7. c#Enum的用法

    public enum ResType { Role = 0, Dept = 1, Group = 2, Site = 3, Org = 4, Sub=8 } 这里定义了一个enum    ResTy ...

  8. 部署到Linux使用VS Code 开发.NET Core 应用程序

    使用VS Code 开发.NET Core 应用程序 部署到Linux 跨平台 使用VS Code 开发.NET Core 应用程序 部署到Linux 跨平台. 前面讲解了VSCode开发调试 .NE ...

  9. U盘1G变8M解决的方法

    本人曾有一个大小为1G的纽曼U盘,在一年前不幸中毒,格式化之后就仅仅剩8M了,然后再也无法正常格式化.尽管仅仅有8M,但总认为扔了可惜,于是乎,就一直束之高阁.昨天突然心血来潮,决定再试一试,纯粹是死 ...

  10. 扯谈网络编程之自己实现ping

    ping是基于ICMP(Internet Control Message Protocol)协议实现的.而ICMP协议是在IP层实现的. ping实际上是发起者发送一个Echo Request(typ ...