Catch That Cow

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

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.

 #include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int b,e;
int Mintim;
struct node
{
int pos,t;
}k,tem;
int vis[+];
queue<node> s;
void bfs()
{
while(!s.empty())
s.pop();
k.pos=b,k.t=;
s.push(k);
while(!s.empty())
{
k=s.front();
s.pop();
if(k.pos==e)
{
Mintim=k.t;
return;
}
if(k.pos<||k.pos>||vis[k.pos]) continue;
vis[k.pos]=;
tem.t=k.t+;
tem.pos=k.pos+;
s.push(tem);
tem.pos=k.pos-;
s.push(tem);
tem.pos=k.pos*;
s.push(tem);
}
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%d%d",&b,&e)!=EOF)
{
memset(vis,,sizeof(vis));
bfs();
printf("%d\n",Mintim);
}
return ;
}
 
 

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. POJ3279 Catch That Cow(BFS)

    本文来源于:http://blog.csdn.net/svitter 意甲冠军:给你一个数字n, 一个数字k.分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x.求主人找到 ...

  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. 如何实现SQL事务的提交,又不对外进行污染(2)

    紧接着上文,这里主要记录事务操作,实现多实体的功能 在SqlTran类中添加方法如下: 1.两个不同实体类型的事务方法: /// <summary> /// 执行事务(事务中不同实体) / ...

  2. TypeError: not enough arguments for format string

    到一个问题,表示100% 的时候,出现这个问题. 因为python语法会认为是你需要转移符,这个时候你可以选择100%% 来表示

  3. Django 403错误:CSRF verification failed. Request aborted

    网上有解决办法,我自己的组合是: 一,FORM加标识 <form action="" method="post"> {% csrf_token %} ...

  4. 编程是一项需要时间来掌握的技能(Anders的经验)

    Anders现在每天仍然花大量的时间实际写代码,并且未来会一直写下去.他很清醒的知道自己长处:“相比起管理,我还是更擅长于编程.” 事实上,Anders的成名之作Turbo Pascal通过出售软件授 ...

  5. 统计useragent和页面情况

  6. 通过基于 Linux 的软件 VPN 设备连接到 Windows Azure 虚拟网络

    摘要 本文章将说明如何通过使用软件 VPN 设备,将本地办公室或站点连接到 Windows Azure 虚拟网络.在构建原型或在构建可以尽快利用云的"开发/测试"工作流时,软件VP ...

  7. 【POJ2196】Specialized Four-Digit Numbers(暴力打表)

    一道水题,只要会复制粘贴就好! #include <iostream> #include <cstring> #include <cstdlib> #include ...

  8. ios实现程序切入后台,实现后台任务

    首先,iOS 会再持续切入后台,给我们5秒钟的时间去处理相关数据,5秒后,程序不会再执行任何代码,处于挂起状态. // 项目需求,按下Home切换后台后向服务器传一些数据,废话不多说,直接上代码 /* ...

  9. 我的第一个Servlet

    学了一个学期JEE,明天就要考试了. 在3月份自己開始准备去努力的复习考研的高数还有英语等学科. 结果到如今才发现,虽说是考的计算机(本专业的)可是考研和技不可兼得. 想想自己没准备考研的时候的每天大 ...

  10. fatal error: malformed or corrupted AST file: &#39;Unable to load module &quot;/Users/apple/Library/Developer

    在同一时候安装使用Xcode5, Xcode6之后, 常常遇到这个问题. fatal error: malformed or corrupted AST file: 'Unable to load m ...