http://tjuacm.chaosheng.top/problem.php?id=1269
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 100010;
int n, k;
bool vis[N];
struct pos{
    int x;
    int step;
};
void bfs(){
    queue<pos> q;
    pos f;
    f.x = n;
    f.step = 0;
    q.push(f);
    vis[f.x] = true;
    while(q.size()){
        f = q.front();
        q.pop();
        //满足条件
        if(f.x == k){
            printf("%d\n", f.step);
            return ;
        }
        pos v;
        for(int i = 0; i < 3; i++){
            if(i == 0){
                v.x = f.x - 1;
            }else if(i == 1){
                v.x = f.x + 1;
            }else{
                v.x = f.x * 2;
            }
            if(v.x >= 0 && v.x < N && !vis[v.x]){
                vis[v.x] = true;
                v.step = f.step + 1;
                q.push(v);
            }
        }        
    }
}
int main(){
    while(cin >> n >> k){
        memset(vis, false, sizeof(vis));
        bfs();
    }
    return 0;
}
 京公网安备 11010502036488号
京公网安备 11010502036488号