POJ3278

//1332K 110ms
#include <iostream>
#include <queue>
using namespace std;

int main()
{
	int a[100001],b[100001] = {0},m,n,f;
    queue<int>x;

	cin >> m >> n;
	a[m] = 0;
    x.push(m);
	while (!x.empty())
	{
       f = x.front();
	   x.pop();
	   if ( f == n ) break;
	   if ( f-1 >= 0 && b[f-1] == 0 )
	   {
		   x.push(f-1);
		   a[f-1] = a[f] + 1;
		   b[f-1] = 1;
	   }
	   if ( f+1 <= 100000 && b[f+1] == 0 )
	   {
		   x.push(f+1);
		   a[f+1] = a[f] + 1;
		    b[f+1] = 1;
	   }
	   if ( 2*f <= 100000 && b[2*f] == 0 )
	   {
		   x.push(2*f);
		   a[2*f] = a[f] + 1;
		   b[2*f] = 1;
	   }
	}
	cout << a[f] << endl;
	return 0;
}


 

你可能感兴趣的:(POJ3278)