在一个长度为 nn 的坐标轴上,蒜头君想从 AA 点 移动到 BB 点。他的移动规则如下:
- 向前一步,坐标增加 11。
- 向后一步,坐标减少 11。
- 跳跃一步,使得坐标乘 22。
蒜头君不能移动到坐标小于 00 或大于 nn 的位置。蒜头君想知道从 AA 点移动到 BB 点的最少步数是多少,你能帮他计算出来么?
输入格式
第一行输入三个整数 nn,AA,BB,分别代表坐标轴长度,起始点坐标,终点坐标。(0 \le A, B \le n \le 50000≤A,B≤n≤5000)
输出格式
输出一个整数占一行,代表蒜头要走的最少步数。
#include<stdio.h>
int n,a,b;
int l,r;
int dir[3]={1,-1,2};
int vis[5010];
int in(int x) {
return x>=0&&x<=n;
}
struct node {
int x,d;
}q[5010];
int bfs(int sx) {
l=0,r=0;
struct node t={sx,0};
q[r++]=t;
vis[sx]=1;
while(l<r) {
struct node now=q[l++];
if(now.x==b) return now.d;
int tx;
tx=now.x+1;
if(in(tx)&&!vis[tx]) {
vis[tx]=1;
struct node t={tx,now.d+1};
q[r++]=t;
}
tx=now.x-1;
if(in(tx)&&!vis[tx]) {
vis[tx]=1;
struct node t={tx,now.d+1};
q[r++]=t;
}
tx=(now.x)*2;
if(in(tx)&&!vis[tx]) {
vis[tx]=1;
struct node t={tx,now.d+1};
q[r++]=t;
}
}
return -1;
}
int main() {
scanf("%d%d%d",&n,&a,&b);
printf("%d",bfs(a));
}