poj3278

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj3278相关的知识,希望对你有一定的参考价值。

#include<iostream>
#define MAX 100001
int john,cow;
int queue[MAX];
int vis[MAX];
int ans;

void bfs()
{
    int tail,head;
    tail=head=0;
    int start=john;
    queue[tail++]=start;
    vis[start]=1;
    while(head!=tail)
    {
        int cur,next;
        cur=queue[head++];
        if(cur==cow)
        {
            ans=vis[cur]-1;
            return;
        }
        next=cur;
        next=cur+1;
        if(next>=0&&next<MAX&&vis[next]==0)
        {
            vis[next]=vis[cur]+1;
            queue[tail++]=next;
        }

        next=cur-1;
        if(next>=0&&next<MAX&&vis[next]==0)
        {
            vis[next]=vis[cur]+1;
            queue[tail++]=next;
        }
        next=2*cur;
        if(next>=0&&next<MAX&&vis[next]==0)
        {
            vis[next]=vis[cur]+1;
            queue[tail++]=next;
        }
    }
}

int main()
{
    //freopen("input.txt","r",stdin);
    std::cin>>john>>cow;
    for(int i=0;i<MAX;i++)
        vis[i]=0;
    bfs();
    std::cout<<ans;
    return 0;
}

 

以上是关于poj3278的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3278 Catch That Cow(简单BFS)

POJ3278_Catch that cow

poj3278 Catch That Cow

POJ3278:Catch That Cow

POJ 3278 Catch That Cow

POJ 3278 Catch That Cow