-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemoticon.java
More file actions
56 lines (45 loc) · 1.08 KB
/
Copy pathemoticon.java
File metadata and controls
56 lines (45 loc) · 1.08 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
53
54
55
56
package graph;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class emoticon {
public static void main(String args[]) {
//#14226¹ø_À̸ðƼÄÜ
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] d = new int[n+1][n+1];
for (int i=0; i<=n; i++) {
Arrays.fill(d[i], -1);
}
Queue<Integer> q = new LinkedList<Integer>();
q.add(1);
q.add(0);
d[1][0] = 0;
while (!q.isEmpty()) {
int s = q.remove();
int c = q.remove();
if (d[s][s] == -1) {
d[s][s] = d[s][c] + 1;
q.add(s); q.add(s);
}
if (s+c <= n && d[s+c][c] == -1) {
d[s+c][c] = d[s][c] + 1;
q.add(s+c); q.add(c);
}
if (s-1 >= 0 && d[s-1][c] == -1) {
d[s-1][c] = d[s][c] + 1;
q.add(s-1); q.add(c);
}
}
int ans = -1;
for (int i=0; i<=n; i++) {
if (d[n][i] != -1) {
if (ans == -1 || ans > d[n][i]) {
ans = d[n][i];
}
}
}
System.out.println(ans);
}
}