http://poj.org/problem?id=3278(bfs)

时间:2022-02-15 03:53:35
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&colon;&sol;&sol;poj&period;org&sol;problem&quest;id&equals;3278

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

  2. poj 1651 http&colon;&sol;&sol;poj&period;org&sol;problem&quest;id&equals;1651

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

  3. poj-3056 http&colon;&sol;&sol;poj&period;org&sol;problem&quest;id&equals;3056

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

  4. poj 1679 http&colon;&sol;&sol;poj&period;org&sol;problem&quest;id&equals;1679

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

  5. poj 1915 http&colon;&sol;&sol;poj&period;org&sol;problem&quest;id&equals;1915

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

  6. 最小生成树 10&period;1&period;5&period;253 1505 poj 1258 http&colon;&sol;&sol;poj&period;org&sol;problem&quest;id&equals;1258

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

  7. Roadblocks http&colon;&sol;&sol;poj&period;org&sol;problem&quest;id&equals;3255

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

  8. http&colon;&sol;&sol;poj&period;org&sol;problem&quest;id&equals;2253

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

  9. 线段树 (区间更新,区间查询) poj http&colon;&sol;&sol;poj&period;org&sol;problem&quest;id&equals;3468

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

随机推荐

  1. python学习09——字典(3)

    今天写了一道python字典题目,用了上次字典(2)中的方法,代码如下: json = {', 'IP':'10.0.0.1'} def find_value(themap, word): if wo ...

  2. java匿名对象&lowbar;面向对象

    class Student{ public void tell(){ System.out.println("Hello jikexueyuan"); } public void ...

  3. 初试visual studio2012的新型数据库LocalDB

    http://www.cnblogs.com/zhangran/archive/2012/08/21/2649200.html 今天在vs2012里面打开以前的mvc3项目,结果弹出警告说在vs201 ...

  4. Junit手动&sol;自动加载spring配置文件

    分配置文件在classpath下和web-inf下两种情况的加载: ApplicationContext context = new FileSystemXmlApplicationContext(& ...

  5. PHP 易出问题记录

    PHP foreach引用缺陷 <?php $array = array(1, 2, 3); foreach ($array as &$v) {} foreach ($array as ...

  6. JavaScript中,关于new的那些事

    这篇文章是自己对new学习过程中的一些理解,有不对的地方希望指出,接受组织的批评教育. 导火线,前段时间学习jQuery的时候,看到源码中有这样一段: jQuery = function(select ...

  7. Android OpenGL ES(十一)绘制一个20面体 &period;

    前面介绍了OpenGL ES所有能够绘制的基本图形,点,线段和三角形.其它所有复杂的2D或3D图形都是由这些基本图形构成. 本例介绍如何使用三角形构造一个正20面体.一个正20面体,有12个顶点,20 ...

  8. 布隆&lpar;Bloom&rpar;过滤器 JAVA实现

    前言 Bloom过滤器,通过将字符串映射为信息指纹从而节省了空间.Bloom过滤器的原理为,将一个字符串通过一定算法映射为八个Hash值,将八个Hash值对应位置的Bitset位进行填充.在进行校验的 ...

  9. Unable to load script from assets &&num;39&semi;index&period;android&period;bundle&&num;39&semi;&period;make sure you bundle is packaged correctly

    解决此问题 以下方法每次都需要执行命令2才能更新 1.创建assets目录 mkdir android/app/src/main/assets 2.执行命令 react-native bundle - ...

  10. poj很好很有层次感(转)

    OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 30 ...