알고리즘/Backjoon
[C++] 16953 - A → B
ShinDongJun
2020. 1. 29. 09:33
문제 링크 : https://www.acmicpc.net/problem/16953
16953번: A → B
첫째 줄에 A, B (1 ≤ A < B ≤ 109)가 주어진다.
www.acmicpc.net
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef pair<int, int> pii;
int A, B;
int BFS()
{
pii p;
queue<pii> Q;
Q.push(pii(B, 1));
while(!Q.empty())
{
p = Q.front();
Q.pop();
if(p.first == A)
return p.second;
if(p.first == 0)
continue;
if(p.first%10 == 1)
Q.push(pii(p.first/10, p.second+1));
if(p.first%2 == 0)
Q.push(pii(p.first/2, p.second+1));
}
return -1;
}
int main()
{
cin >> A >> B;
cout << BFS() << endl;
return 0;
}
|