POJ_3278_广搜

来源:互联网 发布:淘宝店铺统计工具 编辑:程序博客网 时间:2024/04/27 20:48

//============================================================================
// Name        : POJ_3278.cpp
// Author      : tiger
// Version     :
// Copyright   : 广搜即可
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <queue>
#include <stdio.h>
using namespace std;
struct step
{
    int location,steps;
};
#define MAX 200003
bool flag[200005] = {false};
int bfs(step start,int destnation)
{
    if(start.location == destnation)
        return 0;
    queue<step> q;
    q.push(start);

    flag[start.location] = true;
    step next,temp;
    while(!q.empty())
    {
        temp = q.front();
        q.pop();
        next.steps = temp.steps+1;
        //+1
        next.location = temp.location+1;
        if(next.location == destnation)
            return next.steps;
        if(next.location <= MAX && !flag[next.location])
        {
            q.push(next);
            flag[next.location] = true;
        }

        //-1
        next.location = temp.location - 1;
        if(next.location == destnation)
            return next.steps;
        if(next.location >=0 && next.location <= MAX && !flag[next.location])
        {
            q.push(next);
            flag[next.location] = true;
        }
        //*2
        next.location = temp.location*2;
        if(next.location == destnation)
            return next.steps;
        if(next.location <= MAX && !flag[next.location])
        {
            q.push(next);
            flag[next.location] = true;
        }
    }
    return 0;
}

int main() {

    int n,k;
    scanf("%d %d",&n,&k);
    step start;
    start.location = n;
    start.steps = 0;

    printf("%d/n",bfs(start,k));

    return 0;
}