计蒜客 一维坐标的移动 50pts
#include <iostream>
#include <queue>
using namespace std;
struct node {int first, second;} ;
queue <node> q;
int v[5002], n, a, b, cr, st;
void BFS(){
q.push({a, 0});
v[a]=1;
while(q.size()){
cr=q.front().first, st=q.front().second;
q.pop();
if(cr==b){cout << st; exit(0);}
if(cr<n && v[cr+1]==0){q.push({cr+1, st+1}); v[cr+1]=1;}
if(cr>0 && v[cr-1]==0){q.push({cr-1, st+1}); v[cr-1]=1;}
if(cr*2<1+n && v[cr*2]==0){q.push({cr*2, st+1}); v[cr*2]=1;}
}
}
int main(){
cin >> n >> a >> b;
BFS();
}
验证码 y98k