Description
农夫约翰被通知,他的一只奶牛逃逸了!所以他决定,马上出发,尽快把那只奶牛抓回来,他们都站在数轴上.
约翰在N(0≤N≤100000)处,奶牛在K(0≤K≤100000)处.
约翰有两种办法移动,步行和瞬移:步行每秒种可以让约翰从x处走到x+1或x-1处;而瞬移则可让他在1秒内从x处消失,在2x处出现.
然而那只逃逸的奶牛,悲剧地没有发现自己的处境多么糟糕,正站在那儿一动不动.
那么,约翰需要多少时间抓住那只牛呢?
Format
Input
仅有两个整数N和K.
Output
最短的时间.
Samples
输入数据 1
5 17
Copy
输出数据 1
4
Copy
输入数据 2
100000 0
Copy
输出数据 2
100000
#include<bits/stdc++.h>
using namespace std;
bool ln[10201];//,v[1000000001];
long long n,m,a[101][101],u,p,f,s;
long long dx[4]={1,0,-1,0};
long long dy[4]={0,1,0,-1};
char jk;
struct ll{
long long int x,y,z;
};
queue<ll> q;
map<long long,long long> v;
int main(){
cin>>n>>m;
ll e;
e.x=n;
e.y=0;
q.push(e);
while(!q.empty()){
long long tx;
tx=q.front().x+1;
if(q.front().x==m){
cout<<q.front().y;
break;
}
if(v[tx]!=1&&tx<=1e6){
ll t;
t.x=tx;
t.y=q.front().y+1;
q.push(t);
v[tx]=1;
//cout<<q.front().y+1<<" "<<tx<<endl;
}
tx=q.front().x-1;
if(v[tx]!=1&&tx<=1e6){
ll t;
t.x=tx;
t.y=q.front().y+1;
q.push(t);
v[tx]=1;
//cout<<q.front().y+1<<" "<<tx<<endl;
}
tx=q.front().x*2;
if(v[tx]!=1&&tx<=1e6){
ll t;
t.x=tx;
t.y=q.front().y+1;
q.push(t);
v[tx]=1;
//cout<<q.front().y+1<<" "<<tx<<endl;
}
q.pop();
}
//cout<<s;
}
#include<bits/stdc++.h>
using namespace std;
bool ln[10201];//,v[1000000001];
long long n,m,a[101][101],u,p,f,s;
long long dx[4]={1,0,-1,0};
long long dy[4]={0,1,0,-1};
char jk;
struct ll{
long long int x,y,z;
};
queue<ll> q;
map<long long,long long> v;
int main(){
cin>>n>>m;
ll e;
e.x=n;
e.y=0;
q.push(e);
while(!q.empty()){
long long tx;
tx=q.front().x+1;
if(q.front().x==m){
cout<<q.front().y;
break;
}
if(v[tx]!=1&&tx<=1e6){
ll t;
t.x=tx;
t.y=q.front().y+1;
q.push(t);
v[tx]=1;
//cout<<q.front().y+1<<" "<<tx<<endl;
}
tx=q.front().x-1;
if(v[tx]!=1&&tx<=1e6){
ll t;
t.x=tx;
t.y=q.front().y+1;
q.push(t);
v[tx]=1;
//cout<<q.front().y+1<<" "<<tx<<endl;
}
tx=q.front().x*2;
if(v[tx]!=1&&tx<=1e6){
ll t;
t.x=tx;
t.y=q.front().y+1;
q.push(t);
v[tx]=1;
//cout<<q.front().y+1<<" "<<tx<<endl;
}
q.pop();
}
//cout<<s;
}
#include<bits/stdc++.h>
using namespace std;
bool ln[10201];//,v[1000000001];
long long n,m,a[101][101],u,p,f,s;
long long dx[4]={1,0,-1,0};
long long dy[4]={0,1,0,-1};
char jk;
struct ll{
long long int x,y,z;
};
queue<ll> q;
map<long long,long long> v;
int main(){
cin>>n>>m;
ll e;
e.x=n;
e.y=0;
q.push(e);
while(!q.empty()){
long long tx;
tx=q.front().x+1;
if(q.front().x==m){
cout<<q.front().y;
break;
}
if(v[tx]!=1&&tx<=1e6){
ll t;
t.x=tx;
t.y=q.front().y+1;
q.push(t);
v[tx]=1;
//cout<<q.front().y+1<<" "<<tx<<endl;
}
tx=q.front().x-1;
if(v[tx]!=1&&tx<=1e6){
ll t;
t.x=tx;
t.y=q.front().y+1;
q.push(t);
v[tx]=1;
//cout<<q.front().y+1<<" "<<tx<<endl;
}
tx=q.front().x*2;
if(v[tx]!=1&&tx<=1e6){
ll t;
t.x=tx;
t.y=q.front().y+1;
q.push(t);
v[tx]=1;
//cout<<q.front().y+1<<" "<<tx<<endl;
}
q.pop();
}
//cout<<s;
}