poj3278


Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 71899   Accepted: 22632

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

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.

Source

大致題意:

給定兩個整數n和k

通過 n+1或n-1 或n*2 這3種操作,使得n==k

輸出最少的操作次數

 

bfs思想:節點進行廣度優先搜索的順序。

                                                                         

                               搜索實現方法(非遞歸):

                               算法思想:1.設置一個隊列Q,從頂點出發,遍歷該頂點后讓其進隊;

                                                 2.出隊一個頂點元素,求該頂點的所有鄰接點(對應於此題即FJ的三種走法),

                                                    對於沒有遍歷過的鄰接點遍歷之,並 讓其進隊;

                                                 3.若隊空停止,隊不空時繼續第2步。

代碼一:C++STL&&bfs版本:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define N 100010
int step[N],vis[N];
queue<int>q;
int bfs(int n,int k){
    int now,next;
    step[n]=0;
    vis[n]=1;
    q.push(n);
    while(!q.empty()){
        now=q.front();
        q.pop();
        for(int i=0;i<3;i++){
            if(i==0) next=now-1;
            else if(i==1) next=now+1;
            else if(i==2) next=now*2;
            if(next<0||next>N) continue;
            if(!vis[next]){
                vis[next]=1;
                q.push(next);
                step[next]=step[now]+1; 
            }
            if(next==k) return step[next];
        }
    }
}
int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    if(n>=k) printf("%d\n",n-k);  
    else printf("%d\n",bfs(n,k));
    return 0;
}

代碼二:C語言+bfs+模擬隊列版本

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 100010
struct node{
    int x,step;
}q[N];
int vis[N];
int bfs(int n,int k){
    node now,next;
    int head=0,tail=1;
    q[head].x=n;
    q[head].step=0;
    vis[n]=1;
    while(head<tail){
        now=q[head++];
        for(int i=0;i<3;i++){
            if(i==0) next.x=now.x-1;
            else if(i==1) next.x=now.x+1;
            else if(i==2) next.x=now.x*2;
            if(next.x<0||next.x>N) continue;
            if(!vis[next.x]){
                vis[next.x]=1;
                next.step=now.step+1;
                q[tail++]=next;
            }
            if(next.x==k) return next.step;
        }

    }
}
int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    if(n>=k) printf("%d\n",n-k);  
    else printf("%d\n",bfs(n,k));
    return 0;
}

3.換種風格

#include<iostream>
#include<queue>
using namespace std;
int a[100001],b[100001];
int main()
{
    int m,n,y;
    queue<int>que;
    cin>>m>>n;
    que.push(m);
    a[m]=0;
    while(!que.empty()){
        y=que.front();que.pop();
        b[y]=1;
        if(y==n) break;
        if(y-1>=0&&b[y-1]==0){
            que.push(y-1);
            a[y-1]=a[y]+1;
            b[y-1]=1;
        }
        if(y+1<=100000&&b[y+1]==0){
            que.push(y+1);
            a[y+1]=a[y]+1;
            b[y+1]=1;
        }
        if(2*y<=100000&&b[2*y]==0){
            que.push(2*y);
            a[2*y]=a[y]+1;
            b[2*y]=1;
        }
    }
    cout<<a[y]<<endl;
    return 0;
}

 注:hdu同題

http://acm.hdu.edu.cn/showproblem.php?pid=2717

是輸入若干組數據

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM