题目大意是在给出的规则下最短时间内抓住那只牛:

代码:

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

struct
point{
    int
x;
    int
step;
};

int
used[100005];
int
s,e;

int
dfs(int x)
{

    queue<point> que;
    point tp,ans;
    int
tx;
    ans.x=s;
    ans.step=0;
    used[x]=0;
    que.push(ans);
    while
(!que.empty())
    {

        tp=que.front();
        que.pop();
        tx=tp.x+1;
        if
(tx<0||tx>100000)
            ;

        else

        {

            if
(used[tx])
            {

                used[tx]=0;
                ans.x=tx;
                ans.step=tp.step+1;
                que.push(ans);
                if
(tx==e)
                    return
ans.step;           
            }
        }

        tx=tp.x-1;
        if
(tx<0||tx>100000)
            ;

        else

        {

            if
(used[tx])
            {

                used[tx]=0;
                ans.x=tx;
                ans.step=tp.step+1;
                que.push(ans);
                if
(tx==e)
                    return
ans.step;           
            }
        }

        tx=tp.x*2;
        if
(tx<0||tx>100000)
            ;

        else

        {

            if
(used[tx])
            {

                used[tx]=0;
                ans.x=tx;
                ans.step=tp.step+1;
                que.push(ans);
                if
(tx==e)
                    return
ans.step;           
            }
        }
    }
}


int
main()
{

    while
(cin>>s>>e)
    {

        if
(s==e)
            cout<<'0'<<endl;
        else

        {

            memset(used,1,sizeof(used));
            cout<<dfs(s)<<endl;
        }   
    }

    return
0;
}

posted on 2012-07-15 10:43  xinmenghuairi  阅读(254)  评论(0编辑  收藏  举报