kuangbin专题一:C题,POJ3278:Catch That Cow

POJ3278:Catch That Cow

kuangbin专题一:C题

题目链接:http://poj.org/problem?id=3278

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
题意:给出起点 问最少行动多少次可以到达终点
   行动方式有三种 : -1 +1 *2
AC代码:(BFS)
/*Source Code
Problem: 3278        User: 201616040106
Memory: 988K        Time: 172MS
Language: C++        Result: Accepted

    Source Code*/

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

    #define maxn 110000 
    #define LL long long 

    bool visit[maxn] ; 
    int n , k ; 
    int result ; 

    struct node{
        int pos ; 
        int step ; 
    };
    node st ; 

    void BFS(){
        queue<node>Q ;
        Q.push(st) ; 
        while(!Q.empty()){
            
            node q = Q.front() ; 
            Q.pop() ; 
            if(q.pos == k ){
                result = q.step ; 
                return;
            } 
            node turn ; 
            if(q.pos-1>=0&&!visit[q.pos-1]){
                turn.pos = q.pos-1 ; 
                turn.step = q.step +1 ;
                visit[turn.pos] = 1 ;   
                Q.push(turn) ; 
            }
            if(q.pos+1<=100000&&!visit[q.pos+1]){
                turn.pos = q.pos+1 ; 
                turn.step = q.step +1 ; 
                visit[turn.pos] = 1 ;  
                Q.push(turn) ; 
            }
            if(q.pos*2<=100000&&!visit[q.pos*2]){
                turn.pos = q.pos*2 ; 
                turn.step = q.step +1 ; 
                visit[turn.pos] = 1 ;
                Q.push(turn) ; 
            }
        } 
    }

    int main(){
        while(~scanf("%d%d" , &n , &k)){
            memset(visit , 0 , sizeof(visit)) ; 
            visit[n] = 1 ; 
            st.pos = n ; 
            st.step = 0 ; 
            result = 0 ; 
            BFS() ; 
            
            printf("%d
" , result) ; 
        }
        return 0 ; 
    }

 AC代码:(数组模拟BFS 省去不少时间)

Source Code
Problem: 3278        User: 201616040106
Memory: 1448K        Time: 47MS
Language: C++        Result: Accepted

    Source Code

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;


    int visit[100005],location[100005],step[100005];
    int head, tail;
    int n, k;
    int main(){
        scanf("%d%d", &n, &k);
        tail=0, head=1, location[head]=n,visit[n]=1;
        while(tail<head){
            tail++;
            int x=location[tail];
            if(x==k){
                cout<<step[tail]<<endl;
                return 0;
            }
            if(x-1>=0&&visit[x-1]==0){
                head++; location[head]=x-1; step[head]=step[tail]+1; visit[x-1]=1;
            }
            if(x+1<=100000&&visit[x+1]==0){
                head++; location[head]=x+1; step[head]=step[tail]+1; visit[x+1]=1;
            }
            if(2*x<=100000&&visit[2*x]==0){
                head++; location[head]=2*x; step[head]=step[tail]+1; visit[2*x]=1;
            }
        }
    }

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.
原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7637939.html