안선생의 개발 블로그

C++ 16953 A ->B 본문

백준

C++ 16953 A ->B

안선생 2023. 2. 25. 19:53
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
#include <iostream>
#include <vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
int n, m, k = 0, e = 0;
int bfs(int x)
{
    queue <pair<intint>> a;
    a.push({ x,1 });
    while (!a.empty())
    {
        long long xx = a.front().first;
        int c = a.front().second;
        a.pop();
        if (xx == m)
            return c;
        if (xx * 2 <=m)
        {
            a.push({ xx * 2,c+1 });
        }
        if (xx * 10+1 <= m )
        {
            a.push({xx * 10 + 1,c+1});
        }
    }
    return -1;
}
 
int main()
{
    cin >> n >> m;
    cout << bfs(n);
   
    return 0;
}
 
cs

https://www.acmicpc.net/problem/16953

 

16953번: A → B

첫째 줄에 A, B (1 ≤ A < B ≤ 109)가 주어진다.

www.acmicpc.net

 

'백준' 카테고리의 다른 글

C++ 13565 침투  (1) 2023.02.25
C++ 1303 전쟁 - 전투  (0) 2023.02.25
C++ 11123 양 한마리... 양 두마리...  (0) 2023.02.24
C++ 16948 데스나이트  (0) 2023.02.24
C++ 3184 양  (1) 2023.02.24