题目链接: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. 重载和const参数

    const仅能用于在定义函数签名时,区分是为引用定义参数,还是为指针定义参数.定义基本类型type(如int等),从重载的观点来看,const int和int是相同的. const long& ...

  2. [剑指Offer] 53.表示数值的字符串

    题目描述 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2","-123","3.1 ...

  3. Bootstrap排版类

    类 描述 实例 .lead 使段落突出显示 尝试一下 .small 设定小文本 (设置为父文本的 85% 大小) 尝试一下 .text-left 设定文本左对齐 尝试一下 .text-center 设 ...

  4. BZOJ2151 种树(贪心+堆+链表/wqs二分+动态规划)

    dp容易想到,但没法进一步优化了. 考虑贪心,每次选出价值最大的物品.但这显然是不对的因为会影响其他物品的选择. 于是考虑加上反悔操作.每次选出一个物品后,将其相邻两物品删除,再将原物品价值变为相邻两 ...

  5. OI入门

    学习顺序: 1.高精度计算: 高精度计算(一) 高精度计算练习1 高精度计算(二) 高精度计算练习2 2.递推 3.递归 递归算法 递归练习 4.搜索与回溯 搜索与回溯算法(一) 搜索与回溯练习(一) ...

  6. Navcat中Oracle连接的坑-Instant Client

    报错信息: 官方下载Instant Client下载: http://www.oracle.com/technetwork/cn/topics/intel-macsoft-102027-zhs.htm ...

  7. bzoj1901: Zju2112 Dynamic Rankings(BIT套主席树)

    带修改的题主席树不记录前缀,只记录单点,用BIT统计前缀.  对于BIT上每一个点建一棵主席树,修改和询问的时候用BIT跑,在主席树上做就行了.  3k4人AC的题#256...应该不算慢 #incl ...

  8. Eclipse配置web开发环境

    eclipse的web配置: eclipse:Eclipse Java EE IDE for Web Developers. Version: Helios Service Release 1 下载地 ...

  9. 剑桥offer(41~50)

    41.题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). class Solution { pub ...

  10. ACE中UDP通信

    转载于:http://www.cnblogs.com/TianFang/archive/2006/12/07/585205.html udp是一种无连接的协议,提供无连接不可靠的服务. 在ace中,通 ...