码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3278 Catch That Cow

时间:2014-11-17 15:33:46      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   sp   for   div   on   

#include<stdio.h>
#include<iostream>
#include<cstdio>
#include<queue>
#include<memory.h>
#define maxn 100005
using 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

标签:style   blog   io   color   os   sp   for   div   on   

原文地址:http://www.cnblogs.com/qscqesze/p/4103452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!