문제풀이/백준

백준 16953 - A -> B

레옹 2021. 10. 10. 15:50
#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
using namespace std;
long int A,B;
long int minCount = 100000000;
void DFS(long int num, bool gup, long int count) {
	count++;
	if (gup) {
		num *= 2;
	}
	else {
		num *= 10;
		num += 1;
	}


	if (num == B) {
		if (count < minCount)
			minCount = count;
		return;
	}
	if (num < B) {
		DFS(num, false, count);
		DFS(num, true, count);
	}
	else {
		return;
	}
}
int main() {
	cin >> A >> B;
	DFS(A, true, 0);
	DFS(A, false, 0);

	if (minCount == 100000000) {
		cout << "-1" << endl;
	}
	else
	cout << minCount + 1 << endl;
	return 0;
}