在一个长度为 n 的坐标轴上,蒜头君想从 A 点 移动到 B 点。他的移动规则如下:
- 向前一步,坐标增加 1。
- 向后一步,坐标减少 1。
- 跳跃一步,使得坐标乘 2。
蒜头君不能移动到坐标小于 0 或大于 n 的位置。蒜头想知道从 A 点移动到 B 点的最少步数是多少,你能帮他计算出来么?
输入格式
第一行输入三个整数 n,A,B,分别代表坐标轴长度,起始点坐标,终点坐标。(50000≤A,B≤n≤5000)
输出格式
输出一个整数占一行,代表蒜头要走的最少步数。
样例输入
10 2 7
样例输出
3
#include<bits/stdc++.h>
using namespace std;
int vis[5005]={0};
struct node{
int x;
int step;
node(int x,int step){
this->x = x;
this->step = step;
}
};
int main(){
int n,a,b;
cin>>n>>a>>b;
queue<node> q;
q.push(node(a,0));
vis[a] = 1;
while(q.size()){
node x = q.front();
q.pop();
if(x.x==b){
cout<<x.step<<endl;
break;
}
if(x.x-1>=0&&!vis[x.x-1]){
q.push(node(x.x-1,x.step+1));
vis[x.x-1] = 1;
}
if(x.x+1<=n&&!vis[x.x+1]){
q.push(node(x.x+1,x.step+1));
vis[x.x+1] = 1;
}
if(x.x*2<=n&&!vis[x.x*2]){
q.push(node(x.x*2,x.step+1));
vis[x.x*2] = 1;
}
}
}