poj 3278 Catch That Cow

点击打开链接

Catch That Cow

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 62146   Accepted: 19460

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 - 1 or + 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

题目大意:就是给你两个数 n 和 k 让你通过以下三种方法使得 n -> k;

1) n + 1;

2) n - 1;

3)n * 2;

看怎么样通过上述三种方法使得 n 变成 k 而且需要的步骤最少;

样例解释:

5 17

5 -> 10(1) -> 9(2) -> 18(3) -> 17(4)

所以只需要 4 步

解题思路:这是广搜的类型,所以需要bfs就行了,具体注意的问题会在代码里给出:

/*
Date : 2015-09-08 晚上

Author : ITAK

Motto :

今日的我要超越昨日的我,明日的我要胜过今日的我;
以创作出更好的代码为目标,不断地超越自己。
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn = 100001;

bool vis[maxn];
int n,k;
int fac[maxn];///记录结果
int dis[maxn];//

void bfs()
{
    int front, rear, dx, dy;
    rear = front = 0;
    dis[0] = n, fac[n] = 0;
    vis[n] = 1;
    while(front <= rear)
    {
        dx = dis[front];

        dy = dx - 1;
        if(dy>=0 && !vis[dy])///剪枝 -> dy>=0
        {
            rear++;
            dis[rear] = dy;
            fac[dy] = fac[dx] + 1;
            vis[dy] = 1;
        }
        dy = dx + 1;
        if(dy<maxn && !vis[dy])///剪枝 -> dy < maxn
        {
            rear++;
            dis[rear] = dy;
            fac[dy] = fac[dx] + 1;
            vis[dy] = 1;
        }
        dy = dx*2;
        if(dy<maxn && !vis[dy])///剪枝 -> dy < maxn
        {
            rear++;
            dis[rear] = dy;
            fac[dy] = fac[dx] + 1;
            vis[dy]  = 1;
        }
        if(vis[k])///结束
            break;
        front++;
    }
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        memset(vis, 0, sizeof(vis));
        bfs();
        cout<<fac[k]<<endl;
    }
    return 0;
}
时间: 2024-11-02 05:59:04

poj 3278 Catch That Cow的相关文章

POJ 3278(bfs)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31637   Accepted: 9740 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

《趣题学算法》—第1章1.2节简单的数学计算

1.2 简单的数学计算以上那样利用循环重复将部分数据简单地累加,可以解决很多计数问题.然而,如果计数问题可以通过数学计算直接得出结果,往往可以大大改善算法的时间效率,请看下列问题. 问题1-5 小小度刷礼品图片 7 问题描述 一年一度的百度之星大赛又开始了,这次参赛人数创下了吉尼斯世界纪录.于是,百度之星决定奖励一部分人:所有资格赛提交ID以x结尾的参赛选手将得到精美礼品一份. 小小度同学非常想得到这份礼品,于是他就连续提交了很多次,提交的ID从a连续到b.他想知道能得到多少份礼品,你能帮帮他吗

poj 1703 Find them, Catch them:种类并查集

链接: http://poj.org/problem?id=1703 题目: Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22289   Accepted: 6648 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up

POJ 3176 Cow Bowling:简单DP

Cow Bowling http://poj.org/problem?id=3176 Time Limit: 1000MS Memory Limit: 65536K Description The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-l

poj 3167 Cow Bowling【dp】

这是一道最最基础的dp题目,还记得当时看刘汝佳写的<入门经典>时,就是拿的这个做例子,不过牛人当然是一笔带过这么简单的例子. 状态转移方程为:dp[i][j]+=max(dp[i+1][j],dp[i+1][j+1]),如果要记录路径也简单,另外再用一个数组专门存放原始数组就好 原三角形是 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 dp三角形是 30 23 21 20 13 10 7 12 10 10 4  5    2   6    5 如果要记录路径,就是从dp[1][1

poj 1985 Cow Marathon

点击打开poj 1985 思路: 两次bfs找到树的直径 分析: 1 题目给定一棵树,找树上两点最远距离 2 利用两次bfs就可以,第一次bfs从1开始求出到最远的点,然后从这个点再次bfs回来求出ans即可 代码: #include<queue> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> usi

poj 1703 Find them, Catch them

点击打开链接poj 1703 思路: 简单带权并查集 分析: 1 题目要询问给定的x和y是否是同一个动物 2 我们假设如果不同,那么权值为1,否则为0.因此对于给定的x和y,那么如果x和y不在一个集合里面那么就是不确定,否则就可以根据rank来判断是什么关系 代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; co

算法:POJ 2184 Cow Exhibition (dp 转换01背包)

题目大意: 有N个物品,每个物品有属性Si和Fi,-1000 <= Si, Fi <= 1000, 每种物品最 多只能选一次,问怎样选使得物品的所有Si和Fi属性之和最大,并且要求Si之和与Fi之和都不能下于 0. 思路: 这题想了很久都没思路,于是跟前辈请教了下,恍然大悟.把属性Si当做是物品的 费用,Fi当做是价值,然后做01背包即可. 代码: #include<iostream> #include<queue> #include<stack> #inc

POJ题目分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford