[kuangbin带你飞]专题一 简单搜索- C - Catch That Cow

#include 
#include 
#include 
#include 
#include 
using namespace std;
#define INF 0x3f3f3f

int n, k;
long long cnt = INF;
int vis[1000010];
struct node
{
    int x;
    long long st;
    node(int x, int st):x(x), st(st){};
};

int main()
{
    scanf("%d %d", &n, &k);
    queue<node>q;
    node a(n,0);
    vis[n] = 1;
    q.push(a);
    while(!q.empty())
    {
        node p = q.front(); q.pop();
        if(p.x==k){
            cnt = p.st;
            break;
        }
        if(p.x>=1&&vis[p.x-1]==0){
            vis[p.x-1] = 1;
            node b(p.x-1,p.st+1);
            q.push(b);
        }
        if(p.x<k&&vis[p.x+1]==0){
            vis[p.x+1] = 1;
            node b(p.x+1,p.st+1);
            q.push(b);
        }
        if(p.x<k&&vis[p.x*2]==0){
            vis[p.x*2] = 1;
            node b(p.x*2,p.st+1);
            q.push(b);
        }
    }
    printf("%lld\n", cnt);
    return 0;
}

你可能感兴趣的:(kuangbin)