-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstablizeCore.java
More file actions
52 lines (44 loc) · 1.51 KB
/
stablizeCore.java
File metadata and controls
52 lines (44 loc) · 1.51 KB
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
43
44
45
46
47
48
49
50
51
52
import java.util.LinkedList;
import java.util.Queue;
public class stablizeCore {
public static void solution(int input) {
Queue<Integer> queue = new LinkedList<>();
int[] cnt = new int[input + 1];
int[] parent = new int[input + 1];
queue.add(input);
parent[input] = 0;
while (!queue.isEmpty()) {
int value = queue.poll();
if (value == 0 ) break; // 탈출 조건
if (value % 3 == 0 && cnt[value / 3] == 0) {
queue.add(value / 3);
cnt[value / 3] = cnt[value] + 1;
parent[value / 3] = value;
}
if (value % 2 == 0 && cnt[value / 2] == 0) {
queue.add(value / 2);
cnt[value / 2] = cnt[value] + 1;
parent[value / 2] = value;
}
if (value > 1 && cnt[value - 1] == 0) {
queue.add(value - 1);
cnt[value - 1] = cnt[value] + 1;
parent[value - 1] = value;
}
}
System.out.printf("%d\n", cnt[1]);
int[] answer = new int[cnt[1]+1];
int count = cnt[1]+1;
for (int i = 1; i != 0; i = parent[i]) { // 1에서부터 부모로 거슬러 올라감
count--;
answer[count] = i;
}
for (int i = 0; i < answer.length; i++) {
System.out.printf("%d ", answer[i]);
}
}
public static void main(String[] args) {
int input = 10;
solution(input);
}
}