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

    http://en.wikipedia.org/wiki/Security_information_and_event_management http://en.wikipedia.org/wiki/ ...

  2. LeetCode_Unique Binary Search Trees II

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  3. LeetCode_Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  4. pl/sql执行动态sql

    SQL> declare             msql varchar2(200);    begin    loop    msql := 'select * from bfw_test' ...

  5. Uva 1342 - That Nice Euler Circuit

    Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his ...

  6. asp.net mvc4中自定义404

    原文地址:http://www.chuchur.com/asp-net-mvc4-404/ 定义404 方法当然有很多种.不同的方法所展现的形式也不一样,用户所体验也不一样.以下提供2两种 方法一: ...

  7. SCOI2014省选总结

    这一次省选,主要是抱着玩的心态去的,如同高二的那些大神高一的心态一样,只记得在省选之前我们一直在说,这一次我们的目标,就是不爆0,最后也如愿以偿的实现了. 首先,请允许我吐槽一下day1.....da ...

  8. 常用的Eclipse快捷键

    alt+shift+r 修改名字 ctrl+shift+r 查找源类 Eclipse快捷键功能1. [ALT+/]   --->提示此快捷键为用户编辑的好帮手,能为用户提供内容的辅助,不要为记不 ...

  9. android 推断Apk是否签名和 签名是否一致

    推断Apk是否签名 用命令:jarsigner -verify -verbose -certs <apk文件> 假设有Android Debug字樣就是debug 假设已经签名: [证书的 ...

  10. java的Future使用方法

    首先,Future是一个接口,该接口用来返回异步的结果. package com.itbuluoge.mythread; import java.util.ArrayList; import java ...