【17】 UOJ 抓 牛

[Title] Description:
Farmer John is informed, one of his cows escapes! So he decided to start at once, as soon as the bird cows come back again.

They were standing on the number line. John at N, K cows in place. John, there are two ways to move, walk and teleport: to let John go walking per second x + l or xl at from at x; and teleport can make him disappear from x at 1 second, at 2x at appear. However, it would only escape cows, tragically did not discover how bad their situation, standing there motionless.

Well, John how long it takes hold of the cow it?

[Description Input:
only two integers N and K.

[Output] Description:
The shortest time

[] Sample input:
517
[] Output Sample:
4
[time limit, and the range of data Description:
Time: 1s space: 128M

O<=N<=100000

O<=K<=100000


This question is a standard bfs topic, be careful not to cross-border, as well as to this point is not searched.

 

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
int x,y,a,b;
int s[100006];
int q[100006];
int main(){
//    freopen("17.in","r",stdin);
//    freopen("17.out","w",stdout);
    scanf("%d %d",&a,&b);
    if(b==0){ cout<<a; return 0; }
    memset(s,-1,sizeof(s));
    int front=0;
    int rear=0;
    s[a]=0; q[rear++]=a;
    while(s[b]==-1 && front!=rear){
        x=q[front++]; y=x-1;
        if(y>0 && y<=100000 && s[y]==-1){
            s[y]=s[x]+1;
            q[rear++]=y;
        }
        y=x+1;
        if(y>0 && y<=100000 && s[y]==-1){
            s[y]=s[x]+1;
            q[rear++]=y;
        }
        y=x*2;
        if(y>0 && y<=100000 && s[y]==-1){
            s[y]=s[x]+1;
            q[rear++]=y;
        }
    }
    cout<<s[b];
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11129370.html
Recommended