Skip to content

Commit a0cee53

Browse files
committed
[Silver II] Title: A → B, Time: 132 ms, Memory: 16200 KB -BaekjoonHub
1 parent e51aa0b commit a0cee53

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
public class Main {
5+
static long answer =Long.MAX_VALUE;
6+
public static void main(String[] args) throws IOException {
7+
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
10+
StringTokenizer st = new StringTokenizer(br.readLine());
11+
12+
long A = Integer.parseInt(st.nextToken());
13+
long B = Integer.parseInt(st.nextToken());
14+
15+
dfs(0,A,B);
16+
if(answer == Long.MAX_VALUE) {
17+
System.out.println(-1);
18+
System.exit(0);
19+
}
20+
System.out.println(answer+1);
21+
}
22+
23+
static void dfs(long depth, long start, long end) {
24+
if(start == end ){
25+
if(depth < answer)
26+
answer =depth;
27+
28+
return;
29+
}
30+
if(start > end) {
31+
return ;
32+
}
33+
34+
dfs(depth+1, start*10+1, end);
35+
dfs(depth+1, start*2, end);
36+
}
37+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# [Silver II] A → B - 16953
2+
3+
[문제 링크](https://www.acmicpc.net/problem/16953)
4+
5+
### 성능 요약
6+
7+
메모리: 16200 KB, 시간: 132 ms
8+
9+
### 분류
10+
11+
너비 우선 탐색, 그래프 이론, 그래프 탐색, 그리디 알고리즘
12+
13+
### 제출 일자
14+
15+
2024년 11월 15일 16:14:45
16+
17+
### 문제 설명
18+
19+
<p>정수 A를 B로 바꾸려고 한다. 가능한 연산은 다음과 같은 두 가지이다.</p>
20+
21+
<ul>
22+
<li>2를 곱한다.</li>
23+
<li>1을 수의 가장 오른쪽에 추가한다. </li>
24+
</ul>
25+
26+
<p>A를 B로 바꾸는데 필요한 연산의 최솟값을 구해보자.</p>
27+
28+
### 입력
29+
30+
<p>첫째 줄에 A, B (1 ≤ A < B ≤ 10<sup>9</sup>)가 주어진다.</p>
31+
32+
### 출력
33+
34+
<p>A를 B로 바꾸는데 필요한 연산의 최솟값에 1을 더한 값을 출력한다. 만들 수 없는 경우에는 -1을 출력한다.</p>
35+

0 commit comments

Comments
 (0)