POJ3278——Catch That Cow

题目大意:给你两个整数,n和k,n有三种方式移动,n+1,n-1,n*2,  最快让n==k  ;

解题方法:宽度搜索,剪枝,三口搜索   

技巧: 

        ■         数组要开的大

        ■          注意剪枝,不然 RE

                                                            Source Code
                                            Problem: 3278		User: Smile_7
                                            Memory: 1324K		Time: 32MS
                                            Language: C++		Result: Accepted


    #include <iostream>
    #include <cstring>

    using namespace std;

    const int MAX = 200030;

    typedef class
    {
    public:
        int x;
        int step;
    }point;

    int n , k ;
    bool vist[MAX];
    point queue[MAX];

    void bfs()
    {
        int head , tail;
        queue[head = tail = 0].x = n;
        queue[tail++].step = 0;

        vist[n] = true;

        while(head < tail)
        {
            point w = queue[head++];

            if(w.x == k)
            {
                cout<<w.step<<endl;
                break;
            }

            if(w.x-1 >=  0 && !vist[w.x-1])
            {
                vist[w.x-1] = true;
                queue[tail].x = w.x-1;
                queue[tail++].step = w.step+1;
            }
            if(w.x <= k && !vist[w.x+1])
            {
                vist[w.x+1] = true;
                queue[tail].x = w.x+1;
                queue[tail++].step = w.step+1;
            }
            if(w.x <= k && !vist[2*w.x])
            {
                vist[2*w.x] = true;
                queue[tail].x = 2*w.x;
                queue[tail++].step = w.step+1;
            }
        }

        return;
    }

    int main()
    {
        while(cin>>n>>k)
        {
            memset(vist,false,sizeof(vist));
            bfs();
        }
        return 0;
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值