1646: [Usaco2007 Open]Catch That Cow 抓住那只牛

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 1223  Solved: 583
[Submit][Status][Discuss]

Description

    农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上幽发,尽快把那只奶牛抓回来.
    他们都站在数轴上.约翰在N(O≤N≤100000)处,奶牛在K(O≤K≤100000)处.约翰有
两种办法移动,步行和瞬移:步行每秒种可以让约翰从z处走到x+l或x-l处;而瞬移则可让他在1秒内从x处消失,在2x处出现.然而那只逃逸的奶牛,悲剧地没有发现自己的处境多么糟糕,正站在那儿一动不动.
    那么,约翰需要多少时间抓住那只牛呢?

Input

* Line 1: Two space-separated integers: N and K

    仅有两个整数N和K.

Output

* Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

    最短的时间.

Sample Input

5 17

Sample Output

4

搜索入门经典题

其实就是暴力

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
typedef struct
{int x;int time;
}state;
state now, temp;
queue<state> q;
int flag[150005];
int main(void)
{int n, m;while(scanf("%d%d", &n, &m)!=EOF){if(n>=m){printf("%d\n", n-m);continue;}memset(flag, 0, sizeof(flag));now.x = n, now.time = 0;q.push(now);flag[n] = 1;while(q.empty()==0){now = q.front();q.pop();if(now.x==m){while(q.empty()==0)q.pop();break;}if(now.x*2<=150000 && flag[now.x*2]==0){temp.x = now.x*2;temp.time = now.time+1;flag[temp.x] = 1;q.push(temp);}if(now.x-1>0 && flag[now.x-1]==0){temp.x = now.x-1;temp.time = now.time+1;flag[temp.x] = 1;q.push(temp);}if(now.x+1<=100000 && flag[now.x+1]==0){temp.x = now.x+1;temp.time = now.time+1;flag[temp.x] = 1;q.push(temp);}}printf("%d\n", now.time);}return 0;
}

poj 3278 bzoj 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛(BFS)相关推荐

  1. BZOJ 1646: [Usaco2007 Open]Catch That Cow

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1646 这道题我刚开始就直接硬生生地广搜,然后妥妥地TLE了,后来乖乖地加了一些剪枝... ...

  2. Catch That Cow抓住那只奶牛(BFS,广度优先搜索)

    # **Catch That Cow(抓住那只奶牛)** [题意]:农场主要去追一直逃跑的牛,每次移动有三种选择,位置加一,位置减一,位置乘二,要找到最快的方法让农夫追到牛(也就是移动的次数最少,这个 ...

  3. Catch That Cow(抓住那头牛C++)

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

  4. poj 3268 bzoj 1631: [Usaco2007 Feb]Cow Party(最短路)

    1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 855  Solved: 613 [Subm ...

  5. Catch That Cow(农夫和牛)(BFS入门)(详解)

    题目: 题目大意: 输入n和k表示农夫和牛的位置,n和k在区间[0,100000]间 农夫有三种方式进行移动,每种方式需要的时间相同,都是一分钟 1.向前移动一米(+1) 2.向后移动一米(-1) 3 ...

  6. C - Catch That Cow POJ - 3278

    C - Catch That Cow POJ - 3278 首先是大暴搜+最优化剪枝(当当前搜索情况不如已有答案优时,返回) #include<cstdio> #include<al ...

  7. POJ 3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 30924   Accepted: 9536 D ...

  8. POJ 3278 Catch That Cow BFS

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32071   Accepted: 9866 D ...

  9. POJ 3278 Catch That Cow(BFS)

    题目网址:http://poj.org/problem?id=3278 题目: Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Tot ...

最新文章

  1. 动态代理及工厂的简单实现
  2. Elasticsearch技术解析与实战(五)Document解析
  3. 分析Java核心转储
  4. SSH中一些典型的问题
  5. php基本类,php入门一之十大基本类型
  6. mysql示例employees数据库
  7. jquery html app,jquery .html()vs .append()
  8. lopatkin俄大神精简中文系统Windows 10 Pro 19041.21 20H1 Release x86-x64 ZH-CN SMS
  9. vue 安装 axios
  10. html5 后端模板 免费,推荐12个非常不错而且免费的后台管理HTML模板
  11. JAVA——斐波那契数列(递归)
  12. 微信服务号添加小程序
  13. Halcon学习之缺陷检测-凸点检测
  14. Riverbed破解数字性能密码,引领云时代数字变革
  15. 根据关键词采集文章(按关键词采集数据)
  16. 使用主题背景编辑器设计应用主题背景
  17. egg项目部署及PM2的使用
  18. SystemVerilog学习笔记1 ---《数据类型》
  19. 13年android手机top,2013年1月安卓热门机型Top20
  20. Android实现车辆检测(含Android源码 可实时运行)

热门文章

  1. 在哪里学python-在合肥哪里可以学Python
  2. python安装-在Python中安装包的三种方法
  3. python怎么读取csv文件-python如何读取csv数据
  4. python100例详解-Python编程之属性和方法实例详解
  5. MATLAB语音识别
  6. ZS语音识别(智能语音识别工具)V1.3 绿色版
  7. woocommerce产品选项描述修改_简历修改服务:中文修改、英文修改、中英互译、简历定制,名师一对一指导修改!...
  8. JavaScript创建页面节点
  9. SpringBoot 集成log4j2
  10. Vue动态权限路由addRoutes执行初次白屏解决方法。