题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1

Catch That Cow

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7909    Accepted Submission(s):
2498

Problem Description
Farmer John has been informed of the location of a
fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤
100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the
same number line. Farmer John has two modes of transportation: walking and
teleporting.

* Walking: FJ can move from any point X to the points X - 1
or X + 1 in a single minute
* Teleporting: FJ can move from any point X to
the point 2 × X in a single minute.

If the cow, unaware of its pursuit,
does not move at all, how long does it take for Farmer John to retrieve
it?

 
Input
Line 1: Two space-separated integers: N and 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

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.

 
今晚是大年三十除夕夜,在这里祝愿大家新年快乐!在新的一年里,开开心心,工作顺利,学习更上一层楼~n(*≧▽≦*)n
 
题目大意:在一个数轴上有n和k,农夫在n这个位置,奶牛在k那个位置,农夫要抓住奶牛,有两种方法:1、walking即农夫可以走x+1的位置和x-1的位置。
2、teleporting即每分钟可以走到2*x的位置。利用这两种方法,找到最快几步可以到达!
 
解题思路:其实就这三种情况,本来没打算用搜索的,不过发现好多东西都可以灵活运用!我们吧第一种方法看成两个方向,用dir[2]={1,-1,}表示,然后进行广搜!第二种方法自然就要特殊判断一下了!有一个地方要特殊判断一下,就是因为数据比较大,vis[ss.x]可能会超范围导致RE,所以都加一行判断使其保证在题目范围中。
 
详见代码。
 
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue> using namespace std; int dir[]= {,-}; struct node
{
int x,step;
} s,ss; int bfs(int n,int k)
{
queue<node>q,qq;
s.x=n;
s.step=;
int vis[]= {};
q.push(s);
while (!q.empty())
{
s=q.front();
q.pop();
if (s.x==k)
return s.step;
for (int i=; i<; i++)
{
ss.x=s.x+dir[i];
ss.step=s.step+;
if (ss.x>=&&ss.x<=)
if (!vis[ss.x])
{
vis[ss.x]=;
q.push(ss);
}
}
ss.x=s.x*;
ss.step=s.step+;
if (ss.x>=&&ss.x<=)
{
if (!vis[ss.x])
{
vis[ss.x]=;
q.push(ss);
}
}
}
return ;
} int main ()
{
int n,k;
while (~scanf("%d%d",&n,&k))
{
printf ("%d\n",bfs(n,k));
}
return ;
}
 

hdu 2717 Catch That Cow(广搜bfs)的更多相关文章

  1. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

  2. 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 ...

  3. HDU 2717 Catch That Cow(常规bfs)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...

  4. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

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

  5. 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 ...

  6. HDU 2717 Catch That Cow

    简单的广搜: #include <cstdio> #include <queue> using namespace std; ],step[]; int n,start,end ...

  7. 杭电 HDU 2717 Catch That Cow

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

  8. HDU 2717 Catch That Cow (深搜)

    题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants t ...

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

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

随机推荐

  1. Jrebel 工具学习

    Jrebel 可快速实现热部署,节省了大量重启时间,提高了个人开发效率.网上可搜索到破解版. http://baike.baidu.com/link?url=wuzv7Wa7SMUKltJr-dyta ...

  2. (转)linux IO 内核参数调优 之 参数调节和场景分析

    1. pdflush刷新脏数据条件 (linux IO 内核参数调优 之 原理和参数介绍)上一章节讲述了IO内核调优介个重要参数参数. 总结可知cached中的脏数据满足如下几个条件中一个或者多个的时 ...

  3. c++的一些编程技巧和细节

    1.函数形参,如: CreateProcess(                  NULL,                  cmdbuf,                  NULL,      ...

  4. MySQL中的条件语句

    判断学生表中成绩是否小于60,将小于60的学生成绩列为不及格 学生表(student) 字段:姓名(name),学号(主键)(num),性别(sex),成绩(score) select *,if(sc ...

  5. 【python】使用枚举类

    当我们需要定义常量时,一个办法是用大写变量通过整数来定义,例如月份: JAN = 1 FEB = 2 MAR = 3 ... NOV = 11 DEC = 12 好处是简单,缺点是类型是int,并且仍 ...

  6. SP263 PERIOD - Period

    题目描述 For each prefix of a given string S with N characters (each character has an ASCII code between ...

  7. P1483 序列变换

    题目描述 给定一个由n个整数构成的序列,你需要对它进行如下操作: 操作1:输入格式“1 x y”,表示把所有a[kx](k为正整数,kx<=n)都加上y. 操作2:输入格式“2 j”,表示输出a ...

  8. hihoCoder#1838 : 鎕鎕鎕 贪心

    ---题面--- 题解: 神奇的贪心题,,,感觉每次做贪心题都无从下手... 我们首先按照a对所有卡片从小到大排序,然后从1开始,从连续的两张牌中取b最大的,最后一张单出来的也取了. 可以证明,这样的 ...

  9. BZOJ3894:文理分科——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=3894 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理 ...

  10. IP协议简介

    一.IP 1.IP是TCP/IP协议簇中最为核心的协议,所有的TCP.UDP.ICMP及IGMP数据都是以IP数据报格式传输. 2.IP提供不可靠.无连接的数据报传送服务 (1)不可靠:不保证IP数据 ...