algorithm/문제 풀이

99클럽 코테 스터디 18일차 TIL - 백준 16953번 (A -> B)

ssoheeh 2024. 4. 18. 22:54

#include <bits/stdc++.h>

using namespace std;

int A,B;

int bfs(int start){
    queue<pair<long long, long long> > q;

    q.push(pair<long long, long long>(start, 1));
    
    while(!q.empty()){
        pair<long long,long long> x = q.front();
        q.pop();
        //cout<<x<<" ";
        if(x.first==B){
            return x.second;
        }
        if(x.first*2<=B){
            q.push(pair<long long,long long>(x.first*2, x.second+1));
        }

        if(x.first*10+1<=B){
            q.push(pair<long long,long long>(x.first*10+1, x.second+1));
        }
    }
    return -1;
}


int main()
{
    cin.tie(0);
    ios::sync_with_stdio(0);
    cin>>A>>B;
    cout<<bfs(A);

    return 0;
}

숨바꼭질과 비슷한 bfs 풀이

 

 

 

오늘의 회고

  • 알고리즘 문제가 잘 안풀리는 느낌이라 하기가 싫었는데 그래도 꾸준히 푸는 연습을 위해 매일 풀고 있다. 칭찬!
  • 내일(4/19) 학습 예정 : 스프링부트 온라인 강의 듣기