Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 76935   Accepted: 24323

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.
 
题意:给出两个数star,end,给出x-1,x+1,x*2四种运算,使star变成end;
 
思路:直接bfs就好了;
 
代码:
 #include <iostream>
#include <string.h>
#include <queue>
#define MAXN 200000+10
using namespace std; struct Node //***用结构体表示节点,x表示当前值,step记录当前步数
{
int x, step;
}; int star, end, vis[MAXN]; int bfs(int x)
{
Node a, b, c;
a.x = x, a.step = ; //***赋初值
queue<Node>q; //***用队列实现
q.push(a); //***头节点入队
while(!q.empty()) //***如果队列不为空,继续循环
{
b = q.front(); //***取当前队列的头节点做父节点并将其从队列中删除
q.pop();
vis[b.x] = ; //***标记
if(b.x == end) //***如果达到目标,返回步数
{
return b.step;
}
c = b; //***符合条件的儿子入队
if(c.x >= && !vis[c.x-])
{
c.x-=;
c.step++;
vis[c.x]=;
q.push(c);
}
c = b;
if(c.x < end && !vis[c.x+])
{
c.x+=;
c.step++;
vis[c.x]=;
q.push(c);
}
c = b;
if(c.x < end && !vis[*c.x])
{
c.x*=;
c.step++;
vis[c.x]=;
q.push(c);
}
}
return -;
} int main(void)
{
while(cin >> star >> end)
{
if(star >= end) //***如果star>=end,则每次减1,最少需要star-end步
{
cout << star - end << endl;
continue;
}
memset(vis, , sizeof(vis)); //***标记数组清0,不然会对后面的测试产生影响
int ans=bfs(star); //***深搜
cout << ans << endl;
}
return ;
}

http://poj.org/problem?id=3278(bfs)的更多相关文章

  1. POJ3278http://poj.org/problem?id=3278

    http://poj.org/problem?id=3278 题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步 #include<stdio.h> #include&l ...

  2. poj 1651 http://poj.org/problem?id=1651

      http://poj.org/problem?id=1651Multiplication Puzzle   Time Limit: 1000MS   Memory Limit: 65536K To ...

  3. poj-3056 http://poj.org/problem?id=3056

    http://poj.org/problem?id=3056 The Bavarian Beer Party Time Limit: 6000MS   Memory Limit: 65536K Tot ...

  4. poj 1679 http://poj.org/problem?id=1679

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  5. poj 1915 http://poj.org/problem?id=1915

    /**< */#include <stdio.h> #include <string.h> #include <stdlib.h> #include < ...

  6. 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258

    #include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...

  7. Roadblocks http://poj.org/problem?id=3255

    Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best ...

  8. http://poj.org/problem?id=2253

    floyd的应用求每条路径两点之间最大距离的最小值 #include <iostream> #include <cstdio> #include <algorithm&g ...

  9. 线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468

    题目链接 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...

随机推荐

  1. 校友聊NABCD

    特点之一     界面简洁 N:软件的界面是软件成功的必要条件,界面简洁,用户使用方便,就会吸引用户. A:界面可用多种做法做,暂定用C# B:简洁的界面,用户易于理解各项功能,方便使用. C:没有其 ...

  2. windows 下wamp环境2 配置之mysql的安装

    安装配置mysql 5.7 打开mysql官网: https://www.mysql.com/ 点击downloads,然后选择commuity 选择MySQL Community Server选择对 ...

  3. javascript小技巧(非常全)

    事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture();  event.srcE ...

  4. js实现自定义右键菜单--兼容IE、Firefox、Chrome

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  5. python文件头的#-*- coding: utf-8 -*- 的作用

    这一句其实是告诉编辑器,我的代码使用的格式是utf-8,如果没有这句编辑器就会自动去识别代码的文件格式,如果发现文件格式不是utf-8,就有可能去将编码格式转换为utf-8,比如本来是gbk的,编辑器 ...

  6. HTML5 localStorage 的使用

      在前端开发中,我们会常遇到要在两个甚至多个html之间通信,我们可以在url中添加参数,但是当要传递的数据量较大较多时呢,不妨试试html5 的localStorage吧. 1) 检测你的浏览器是 ...

  7. Sqli-LABS通关笔录-17-审计SQL注入

    这个关卡开始好像进入新的知识区了,前面几个是让我们加深对盲注的印象.接下来又是新知识了!皮卡丘,接招吧! 代码是从SQL-libs的第十七关卡扣的. 页面效果如下所示: PHP面完整CODE如下所示: ...

  8. backbone & django csrf_token的问题

    由于这个加入了token的验证,因此在backbone调用Model/Collection的save时会失败,错误403.(这里不讨论劫持重发的问题) 解决方案是:修改xmlHttpRequest的h ...

  9. 1. Smalidea无源码调试android应用

    一.安装smalidea https://github.com/JesusFreke/smali/wiki/smalidea   1. 进入IntelliJ IDEA/Android Studio开始 ...

  10. JavaScript基础介绍

    JavaScript组成 •ECMAScript:解释器.翻译 •DOM:Document Object Model •BOM:Browser Object Model –各组成部分的兼容性,兼容性问 ...