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.
题意:即农场主在起始点n,奶牛在k点,农场主有三种移动方式,即x-1,x+1,x*2,问追到奶牛的最小步数。
思路:直接用广搜(BFS)对每种移动方式能到达的点进行搜索,用结构体,记录每一个点的位置,和到达改点的步数。不过此题有个坑,就是,当农场主的位置和奶牛的起始点在同一位置中,直接输出就可以了,本人多次的WA就是因为没有考虑到这点。
完整代码如下:

include<stdio.h>
include<string.h>
include<math.h>
define MAX 100000+10
bool book[MAX];
struct node{
    int x;
    int s;//代表走到这一步的步数 
}s[MAX],next;//next为下一步 
int bfs(int n,int k)
{
    int i,j;
    int head = 1,tail = 1;
    s[tail].x = n;s[tail].s = 0;
    book[n] = 1;
    tail++;//把起始点放入队列中,不要忘记队尾要加一 
    int tx;
    while(head < tail)
    {
        for(i=1;i<=3;i++)//三种移动方式,分三种来讨论 
        {
            if(i == 1)
                next.x = s[head].x - 1;
            if(i == 2)
                next.x = s[head].x + 1;
            if(i == 3)
                next.x = s[head].x * 2;
            next.s = s[head].s + 1;//由于这一步是由上一步扩展出来的,故这一步要的步数要加一 
            if(next.x < 0 || next.x > 100000)
                continue;
            if(book[next.x] == 0)
            {
                book[next.x] = 1;
                s[tail] = next;
                tail++;
            }
            if(next.x == k)
                return next.s;
        }
        head++;
    }
}
int main(void)
{
    int n,k;    
    scanf("%d %d",&n,&k);
    if(n > k)
        printf("%d\n",n-k);
    else if( n == k)//当农场主的位置和奶牛位置在一起时直接输出,这一步很重要,也可放入bfs中 
        printf("0\n");
    else if(n < k)
        printf("%d\n",bfs(n,k));
    return 0;
}
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int MAX = 500000;
bool used[MAX];
struct node{
    int value;
    int step;
}be,es;
int n,k;
int bfs(){
    memset(used,false,sizeof(used));
    queue<node> q;
    be.value = n;be.step = 0;
    q.push(be);
    used[be.value] = true;
    while(!q.empty()){
        es = q.front();q.pop();
//        printf("%d\n",es.value);
        if(es.value == k){
            return es.step;
        }
        node nx;
        for(int i=1;i<=3;i++){
            nx.step = es.step + 1;
            if(i == 1) nx.value = es.value - 1;
            else if(i == 2) nx.value = es.value + 1;
            else if(i == 3)  nx.value = es.value * 2;
            if(nx.value < 0 || nx.value > 300000)  continue;
            //n 可能比k大
            if(!used[nx.value]){
                used[nx.value] = true;
                q.push(nx);
            }
        }
    }
    return -1;
}
int main(void){
    while(cin >> n >> k){
        cout << bfs() << endl;
    }
    return 0;
}

本题也可以是多组样例输入,不过在POJ中是一组一组数据的输入,故这样就可以ac

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐