Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 48020 | Accepted: 15054 |
Description
Farmer John has been informed of the location of a fugitive(逃亡的;难以捉摸的;短暂的) cow and wants to catch her immediately. He starts at a pointN (0 ≤N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.
* Walking: FJ can move from any point X to the points X - 1 orX+ 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.
If the cow, unaware of its pursuit(追赶;工作), does not move at all, how long does it take for Farmer John to retrieve it?
Input
Output
Sample Input
5 17
Sample Output
4
Hint
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
int i,j,t,n,k,cur;
int visted[200010];
int bfs()
{
memset(visted, 0, sizeof(visted));
int step=0;
queue<int> q,qq,tmp;
q.push(n);
visted[n]=1;
while(!q.empty())
{
while(!q.empty())
{
cur = q.front();
q.pop();
if(cur == k) return step;
if(cur<k)
{
if(visted[2*cur]==0)
{
qq.push(2*cur);
visted[2*cur] = 1;
}
if(visted[cur+1]==0)
{
qq.push(cur+1);
visted[cur+1] = 1;
}
}
if(cur>0)
{
if(visted[cur-1]==0)
{
qq.push(cur-1);
visted[cur-1] = 1;
}
}
}
tmp=qq;
qq=q;
q=tmp;
step++;
}
}
int main()
{
cin>>n>>k;
cout<<bfs()<<endl;
return 0;
}