Natsu_iiiiro

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

题目链接:http://poj.org/problem?id=3278

ac代码:

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
int a,b;
int vis[100005];
queue<int> q;
int bfs(){
  if(a>=b) return a-b;
  int x;
  q.push(a);
  vis[a] = 0;
  while(!q.empty()){
  int t = q.front();q.pop();
  if(t==b) return vis[t];

  for(int i = 0;i<3;i++){
    if(i==0) x = t + 1;
    else if(i==1) x = t - 1;
    else x = t*2;
    if(x<0||x>100005) continue;
    if(vis[x]==-1){
      vis[x] = vis[t] + 1;
      q.push(x);
    }

  }
  }
}


int main(){
  while(scanf("%d%d",&a,&b)!=EOF){
    while(!q.empty()) q.pop();
    memset(vis,-1,sizeof(vis));
    printf("%d\n",bfs());
    }

}

posted on 2019-03-27 12:37  Natsu_iiiiro  阅读(68)  评论(0编辑  收藏  举报