POJ 3278

代码:

#include <iostream>

using namespace std;

int n, m;

int head = 0, tail = 0;

int bfs(int x);

const int N = 100010;

class John

{

public:

    int loc;

    int time;

};

John s[N];

int used[N];

int main()

{

    cin >> n >> m;

    cout << bfs(n);

    system("pause");

    return 0;

}

int bfs(int x)

{

    used[x] = 1;

    s[head].loc = x;    //   s[head]={x,0} ,如果是这样写在POJ上会报错

    s[head].time = 0;

    John J, nJ;

    int i, j, k;

    while (head <= tail)

    {

        J = s[head++];

        for (i = 0; i < 3; i++)  //像迷宫一样使用数组来改变方向,就只能条件判断了(因为有扩大二倍)

        {

            nJ = J;

            if (i == 0)

            {

                nJ.loc -= 1;

            }

            else if (i == 1)

            {

                nJ.loc += 1;

            }

            else

            {

                nJ.loc *= 2;

            }

            if (nJ.loc < 0 || nJ.loc > 100000 || used[nJ.loc])

                continue;

            else

            {

                nJ.time++;

                used[nJ.loc] = 1;

                s[++tail] = nJ;

                if (nJ.loc == m)

                    return nJ.time;

            }

        }

    }

    return 0;

}

好久都没有接触广搜的代码了,写起来十分生疏~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值