首页 > 代码库 > POJ 3278 Catch That Cow

POJ 3278 Catch That Cow

#include<stdio.h>#include<iostream>#include<cstdio>#include<queue>#include<memory.h>#define maxn 100005using namespace std;queue<int> q;int step[maxn];int visit[maxn];int n,k;int head,next;int bfs(){    q.push(n);    step[n]=0;    visit[n]=1;    while(!q.empty())    {        head=q.front();        q.pop();        for(int i=0;i<3;i++)        {            if(i==0)next=head+1;            if(i==1)next=head-1;            if(i==2)next=head*2;            if(next>maxn||next<0)                continue;            if(!visit[next])            {                q.push(next);                step[next]=step[head]+1;                visit[next]=1;            }            if(next==k)                return step[next];        }    }    return -1;}int main(){    memset(step,0,sizeof(step));    memset(visit,0,sizeof(visit));    cin>>n>>k;    cout<<bfs()<<endl;    return 0;}

很基础的一个BFS,我也不知道该怎么写备注= =

其实我感觉暴力的话,这道题会更加快速的A掉的

POJ 3278 Catch That Cow