本文来源于: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. Velocity教程-脚本语法详解(转)

    Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象. 当Veloci ...

  2. [置顶] Ftp客户端概要设计

    Ftp客户端概要设计 1.概述 ftp是基于TCP的文件传输协议,主要是用于控制远程文件,如下载.上传.续传.重命名.删除等.其命令是基于可见字符,易于理解的方式交互的.客户端与服务器端的交互遵循一应 ...

  3. CocoaPods的install和update卡在“Anylyzing dependencies”的问题解决方式[效率]

    问题 最新CocoaPod更新慢得问题,不管是运行pod install还是podupdate都卡在Anylyzing dependencies. 解决方式 事实上原因是运行两个命令时都会升级Coco ...

  4. Face Alignment at 3000FPS(C++版)工程配置

    源地址:http://blog.csdn.net/sunshine_in_moon/article/details/49838245/ 3000FPS是人脸对齐算法,特点是速度快!我利用的是think ...

  5. Songs

    Two Steps From Hell - Strength of a Thousand Men

  6. Lisp: Common Lisp, Racket, Clojure, Emacs Lisp - Hyperpolyglot

    Lisp: Common Lisp, Racket, Clojure, Emacs Lisp - Hyperpolyglot Lisp: Common Lisp, Racket, Clojure, E ...

  7. [Django实战] 第4篇 - 用户认证(用户登录)

    今天来实现用户登录模块 首先,我们创建一个表单(forms.py): from django import forms from django.contrib.auth.models import U ...

  8. ZOJ 3529 A Game Between Alice and Bob(博弈论-sg函数)

    ZOJ 3529 - A Game Between Alice and Bob Time Limit:5000MS     Memory Limit:262144KB     64bit IO For ...

  9. 多项式相乘(C语言)

    //两个多项式的系数f分别存在x[]和y[]中,下标即为次数,*Max表示本多项式最高次数 int MX_double( double x[], int xMax, double y[], int y ...

  10. linux下的开源移动图像监测程序--motion编译与配置

    前几天在网上偶然看到一篇博客,是利用linxu下的开源的motion搭建嵌入式视频动态监控系统,感觉很好很强大于,是就想自己编译移植一下试试. 所谓移动图像监测,简单来说就是利用摄像头定点监测某个区域 ...