题目链接: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());
}
}