POJ 3278 Catch That Cow

  一维的广搜,从当前点到下个点有三种选择,然后求步数即可。用了STL的队列,

然后忘了加using namespace std,检查浪费了时间。

 

/*Accepted 860K 110MS C++ 640B 2012-05-26 20:44:59 */

#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<queue>

using namespace std;

#define MAXN 100005

int d[MAXN], N, K;



void bfs()

{

    queue<int> q;

    memset( d, -1, sizeof d);

    d[N] = 0;

    q.push(N);

    while( !q.empty())

    {

        int x = q.front(); q.pop();

        if( x == K) break;

        int nx[3];

        nx[0] = x * 2; nx[1] = x - 1; nx[2] = x + 1;

        for( int i = 0; i < 3; i ++)

        {

            if( nx[i] >= 0 && nx[i] <= 100000 && d[nx[i]] < 0) {

                d[nx[i]] = d[x] + 1;

                q.push(nx[i]);

            }

        }

    }

}



int main()

{

    while( scanf( "%d%d", &N, &K) == 2)

    {

        bfs();

        printf( "%d\n", d[K]);

    }

    return 0;

}

 

 

 

你可能感兴趣的:(catch)