总时间限制: 
2000ms

内存限制: 
65536kB
描述

农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0<=K<=100000)。农夫有两种移动方式:

1、从X移动到X-1或X+1,每次移动花费一分钟
2、从X移动到2*X,每次移动花费一分钟
 
假设牛没有意识到农夫的行动,站在原地不动。农夫最少要花多少时间才能抓住牛?
输入
两个整数,N和K
输出
一个整数,农夫抓到牛所要花费的最小分钟数
样例输入
5 17
样例输出
4

普通的BFS。要注意剪枝和开大数组。

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
int n,k;
bool vis[];
int pos[];
int step[];
void BFS(){
int hd=,tl=;
pos[++hd]=n;
step[hd]=;
while(hd<=tl){
int now=pos[hd];
if(now==k){
printf("%d\n",step[hd]);
return;
}
int next=now*;
if(next>= && next<= && !vis[next]){
vis[next]=;
pos[++tl]=next;
step[tl]=step[hd]+;}
next=now+;
if(next<= && !vis[next]){
pos[++tl]=next;
vis[next]=;
step[tl]=step[hd]+;}
next=now-;
if(next>= && !vis[next] ){
vis[next]=;
pos[++tl]=next;
step[tl]=step[hd]+;}
hd++;
}
}
int main(){
scanf("%d%d",&n,&k);
BFS();
return ;
}

OpenJudge 2971 抓住那头牛的更多相关文章

  1. noi.openjudge——2971 抓住那头牛

    http://noi.openjudge.cn/ch0205/2971/ 总时间限制:  2000ms 内存限制:  65536kB 描述 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫 ...

  2. noi 2971 抓住那头牛

    2971:抓住那头牛 查看 提交 统计 提问 总时间限制:  2000ms 内存限制:  65536kB 描述 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0<=N ...

  3. OpenJudge 4001:抓住那头牛

    题目链接 题解: 这个题可以用广搜来解决,从农夫到牛的走法每次都有三种选择,定义一个队列,把农夫的节点加进队列,然后以这三种走法找牛,队列先进先出,按顺序直到找到牛的位置. 代码: #include& ...

  4. 【bfs】抓住那头牛

    [题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...

  5. [poj3278]抓住那头牛

    题目描述 Farmer John has been informed of the location of a fugitive cow and wants to catch her immediat ...

  6. POJ-3278 抓住这头牛

    广搜解决. 广搜搜出最短路,直接输出返回就行了. 每个点只搜一次,而且界限进行一次判断. else 语句里面不要用if    else if,这样的话就直走一条路了. #include <ios ...

  7. 双向广搜 codevs 3060 抓住那头奶牛

    codevs 3060 抓住那头奶牛 USACO  时间限制: 1 s  空间限制: 16000 KB  题目等级 : 黄金 Gold   题目描述 Description 农夫约翰被告知一头逃跑奶牛 ...

  8. BZOJ1646: [Usaco2007 Open]Catch That Cow 抓住那只牛

    1646: [Usaco2007 Open]Catch That Cow 抓住那只牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 634  Solved ...

  9. 2014.6.14模拟赛【bzoj1646】[Usaco2007 Open]Catch That Cow 抓住那只牛

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

随机推荐

  1. c++运算符重载和虚函数

    运算符重载与虚函数 单目运算符 接下来都以AClass作为一个类例子介绍 AClass{ int var } 区分后置++与前置++ AClass operator ++ () ++前置 一般设计为返 ...

  2. Linux清空mysql所有数据

    1,删除data目录下所有文件 rm -rf /usr/local/mysql5/data/* 2,mysql_install_db脚本初始化Mysql /usr/local/mysql5/scrip ...

  3. 自定义token,保存到客户端的cookie中,

    自定义token #原理自定义token,放入cookie中,不用存数据库 #token定义方式 >>>>> "加密字符串"|登陆用户id|用户登陆时 ...

  4. Louis Armstrong【路易斯·阿姆斯特朗】

    Louis Armstrong Louis Armstrong had two famous nicknames. 路易斯·阿姆斯特朗有两个著名的绰号. Some people called him ...

  5. North American Invitational Programming Contest (NAIPC) 2016

    (待补) A. Fancy Antiques 爆搜. B. Alternative Bracket Notation C. Greetings! D. Programming Team 0/1分数规划 ...

  6. 【Plus One】cpp

    题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...

  7. leetcode 【 Unique Paths 】python 实现

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  8. 第九届极客大挑战 部分WP

    CODE 和0xpoker分0day 百度 取石子游戏. https://blog.csdn.net/qq_33765907/article/details/51174524 已经说得很详细了,慢慢来 ...

  9. selenium定位下拉菜单好文

    http://www.cnblogs.com/nbkhic/archive/2011/10/23/2221726.html 定位下拉菜单

  10. manjaro无声音

    解决方法:https://forum.manjaro.org/t/no-sound-solved/3517 LOL i feel like such a noob. Fixed my problem ...