hdu2717[BFS+启发式搜索]

题目描述

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?

算法思路

  1. 本题是求最小化问题,一般使用BFS搜索的方式。
  2. 首先,我们发现,向数轴的右边移动可以采用walking和teleporting两种方式。而向数轴的左边一动则一次只能一动1个单位长度。也就是说如果N>=K,那么最小时间就是N-K
  3. 我们只考虑N< K的情况。
  4. 如果农民移动到K右边的地方,则又回到第二点中我们提到的情况,此时加上判断即可。如果一直保持在K的左边,则到达终点的最长时间为K-locate(cur)+step(cur)也可以更新最优解,进行启发式剪枝。再加上一个数组记录搜索过的位置,本题即可得到解。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;

int N,K;
bool visited[200005];

struct status{
    int curx,step;
    status(int a,int b){
        curx = a;
        step = b;
    }
    status(){}
};


void Solve()
{
    if(N>=K){
        //only one way to solve
        printf("%d\n",N-K);
        return;
    }

    memset(visited,false,sizeof(visited));

    status st = status(N,0);
    visited[N] = true;
    status cur,next;
    queue<status>Q;
    Q.push(st);

    while(!Q.empty()){
        cur = Q.front();
        Q.pop();
        //printf("%d %d\n",cur.curx,cur.step);
        if(cur.curx == K){
            printf("%d\n",cur.step);
            return;
        }

        next = status(cur.curx+1,cur.step+1);
        if(!visited[cur.curx+1]){
            Q.push(next);
            visited[cur.curx+1] = true;
        }
        next = status(cur.curx-1,cur.step+1);
        if(next.curx>=0 && !visited[cur.curx-1]){
            Q.push(next);
            visited[cur.curx-1]=true;
        }
        next = status(cur.curx*2,cur.step+1);
        if((next.curx<=K || next.curx+next.step-K<=K-N) && !visited[next.curx]){
            Q.push(next);
            visited[next.curx] = true;
        }
    }
    return;
}

int main()
{
    //freopen("input","r",stdin);
    while(scanf("%d%d",&N,&K)!=EOF){
        Solve();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值