-
Notifications
You must be signed in to change notification settings - Fork 8
/
Main.java
80 lines (57 loc) · 1.92 KB
/
Main.java
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
/**
* BOJ#14226 이모티콘
* https://www.acmicpc.net/problem/14226
*/
public class Main {
static boolean[][] discovered = new boolean[10000][10000];
public static void main(String[] args) throws IOException {
// input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int S = Integer.parseInt(br.readLine());
// solve - bfs
PriorityQueue<Node> queue = new PriorityQueue<Node>();
queue.add(new Node(1, 0, 0));
while (!queue.isEmpty()) {
Node u = queue.poll();
if (u.value == S) {
System.out.println(u.cost);
break;
}
// copy
if(!discovered[u.value][u.value]) {
queue.add(new Node(u.value, u.value, u.cost + 1));
discovered[u.value][u.value] = true;
}
// paste
int addedValue = u.value + u.clipboard;
if (!discovered[addedValue][u.clipboard] && addedValue < 10000) {
queue.add(new Node(addedValue, u.clipboard, u.cost + 1));
discovered[addedValue][u.clipboard] = true;
}
// delete
int deletedValue = u.value - 1;
if (deletedValue >= 0 && !discovered[deletedValue][u.clipboard]) {
queue.add(new Node(deletedValue, u.clipboard, u.cost + 1));
discovered[addedValue][u.clipboard] = true;
}
}
}
}
class Node implements Comparable<Node> {
int value;
int clipboard;
int cost;
Node(int value, int clipboard, int cost) {
this.value = value;
this.clipboard = clipboard;
this.cost = cost;
}
@Override
public int compareTo(Node o) {
return this.cost < o.cost ? -1 : 1;
}
}