zssd

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

poj-3278

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define p 100010
using namespace std;
int visit[p];        
int n,k;

struct Catch{       
    int step;
    int x;
     Catch(int xx,int s):x(xx),step(s){}
};
queue<Catch> q;

int main(){
    scanf("%d %d",&n,&k);
    memset(visit,0,sizeof(visit));       
    q.push(Catch(n,0));
    visit[n]=1;
    while(!q.empty()){
        Catch c=q.front();
        q.pop();
        if(c.x==k)
        {
            printf("%d\n",c.step);
            return 0;
        }
        else
        {
           if(c.x-1>=0&&!visit[c.x-1])
           {
               q.push(Catch(c.x-1,c.step+1));
               visit[c.x-1]=1;
           }
           if(c.x+1<p&&!visit[c.x+1])
           {
              q.push(Catch(c.x+1,c.step+1));
              visit[c.x+1]=1;
           }
           if(c.x*2<p&&!visit[c.x*2])
           {
              q.push(Catch(c.x*2,c.step+1));
               visit[c.x*2]=1;
           }
        }
    }
}
posted on 2019-01-12 22:28  zssd  阅读(162)  评论(0)    收藏  举报