hdu2717(裸bfs广搜)

Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7304 Accepted Submission(s): 2308

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2717
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.





解题思路:

          一道很裸的bfs题。题意是给出farmer的点和cow的点,farmer可以选择每次+1、-1、*2这三种操作,问最少的操作次数可以使得farmer到达cow点。

          重新温习广搜。广搜对于求最优解或最短路很有帮助,虽然它的时间复杂度颇高,要把整棵树的所有路径都存下来,不过据说双向广搜(dbfs)可以把时间复杂度优化到O(n),加上哈希表处理的话,可以达到O(1)。

           本题考虑两种情况:1、 n < k时,有+1、*2两种走法;2、 n > k 时,只有-1。其实最后所求的最少次数相当于询问树节点为k的最短深度。

附图:





完整代码:

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")

typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;

/** Constant List .. **/ //{

const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;

int n , k;
int vis[100001];
struct node
{
    int pos;
    int step;
}q[10001];

bool check(int x)
{
    if(x >= 0 && x <= 100000 && vis[x] == 0)
        return true;
    else
        return false;
}

int bfs()
{
    queue<node> q;
    node temp;
    temp.pos = n;
    temp.step = 0;
    vis[n] = 1;
    q.push(temp);
    while(!q.empty())
    {
        node temp2;
        temp = q.front();
        q.pop();
        temp2.step = temp.step + 1;
        temp2.pos = temp.pos - 1;
        if(check(temp2.pos))
        {
            if(temp2.pos == k)
                return temp2.step;
            q.push(temp2);
            vis[temp2.pos] = 1;
        }
       //cout << temp2.pos << " " << temp2.step << "\t";
        if(temp2.pos < k)
        {
            temp2.pos = temp.pos + 1;
            if(check(temp2.pos))
            {
                if(temp2.pos == k)
                    return temp2.step;
                q.push(temp2);
                vis[temp2.pos] = 1;
            }
            //cout << temp2.pos << " " << temp2.step << "\t";
            temp2.pos = 2 * temp.pos;
            if(check(temp2.pos))
            {
                if(temp2.pos == k)
                    return temp2.step;
                q.push(temp2);
                vis[temp2.pos] = 1;
            }
        }
        //cout << temp2.pos << " " << temp2.step << endl;
    }
}

int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    while(~scanf("%d%d",&n,&k))
    {
        if(k == n)
        {
            printf("0\n");
            continue;
        }
        memset(vis , 0 , sizeof(vis));
        printf("%d\n",bfs());
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值