-
백준 (BOJ) 16953번 A → B알고리즘 풀이/백준(Boj) 2020. 5. 19. 13:44
문제 : https://www.acmicpc.net/problem/16953
16953번: A → B
첫째 줄에 A, B (1 ≤ A < B ≤ 109)가 주어진다.
www.acmicpc.net
풀이 :
queue를 통해 하나씩 넣어보면서 해결하였다. 두 경우 c1과 c2를 만들어서 큐에 넣고 해당 값들이 b보다 크다면 의미가
없으므로 넣으면 안 된다.
코드 ( C++ )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters#include <string> #include <algorithm> #include <vector> #include <iostream> #include <queue> using namespace std; typedef long long int lli; int main() { int a, b; cin >> a >> b; queue<pair<lli,lli>> q; q.push({ a,1 }); lli ans = -1; while (!q.empty()) { lli x = q.front().first; lli cnt = q.front().second; q.pop(); if (x == b) { ans = cnt; break; } lli c1 = x * 2; lli c2 = x * 10 + 1; if (c1 <= b) q.push({ c1,cnt+1 }); if (c2 <= b) q.push({ c2,cnt+1 }); } cout << ans << "\n"; return 0; } '알고리즘 풀이 > 백준(Boj)' 카테고리의 다른 글
백준(BOJ) 16951 블록 놀이 (0) 2020.06.09 백준 (BOJ) 16958번 텔레포트 (0) 2020.06.02 백준(BOJ) 16943번 숫자 재배치 (0) 2020.05.04 백준(BOJ) 2174번 로봇 시뮬레이션 (0) 2020.04.20 백준(BOJ) 17085번 십자가 2개 놓기 (수정) (0) 2020.04.18