SDJ( 수돈재 아님 ㅎ )

[C++] 16953 - A → B 본문

알고리즘/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<intint> 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;
}

'알고리즘 > Backjoon' 카테고리의 다른 글

[C++] 1351 - 무한 수열  (0) 2020.01.30
[C++] 1038 - 감소하는 수  (0) 2020.01.29
[C++] 17425 - 약수의 합  (0) 2020.01.29
[C++] 5014 - 스타트링크  (0) 2020.01.29
[C++] 12852 - 1로 만들기 2  (0) 2020.01.29
Comments