-
Notifications
You must be signed in to change notification settings - Fork 6
/
dongwoo.java
79 lines (75 loc) · 2.21 KB
/
dongwoo.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class dongwoo {
static int[][][] memo;
static int[] command;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] temp = br.readLine().replace(" ", "").toCharArray();
command = new int[temp.length];
memo = new int[5][5][temp.length];
for (int i = 0; i < temp.length; i++) {
command[i] = temp[i] - '0';
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
Arrays.fill(memo[i][j], Integer.MAX_VALUE);
}
}
int i = 0;
memo[0][0][0] = 0;
while (command[i] != 0) {
for (int j = 0; j < 5; j++) {
for (int k = j; k < 5; k++) {
if (memo[j][k][i] != Integer.MAX_VALUE) {
int smaller = j;
int bigger = k;
if (command[i] >= bigger) {
memo[j][command[i]][i + 1] = Math.min(memo[j][k][i] + getForce(k, command[i]),
memo[j][command[i]][i + 1]);
memo[k][command[i]][i + 1] = Math.min(memo[j][k][i] + getForce(j, command[i]),
memo[k][command[i]][i + 1]);
continue;
}
if (command[i] <= smaller) {
memo[command[i]][j][i + 1] = Math.min(memo[command[i]][j][i + 1],
memo[j][k][i] + getForce(k, command[i]));
memo[command[i]][k][i + 1] = Math.min(memo[j][k][i] + getForce(j, command[i]),
memo[command[i]][k][i + 1]);
continue;
}
if (smaller < command[i] && command[i] < bigger) {
memo[smaller][command[i]][i + 1] = Math.min(memo[j][k][i] + getForce(k, command[i]),
memo[smaller][command[i]][i + 1]);
memo[command[i]][bigger][i + 1] = Math.min(memo[j][k][i] + getForce(j, command[i]),
memo[command[i]][bigger][i + 1]);
continue;
}
}
}
}
i++;
}
int res = Integer.MAX_VALUE;
for (int j = 0; j < 5; j++) {
for (int k = 0; k < 5; k++) {
res = Math.min(memo[j][k][command.length - 1], res);
}
}
System.out.println(res);
}
private static int getForce(int start, int to) {
if (start == to) {
return 1;
}
if (start == 0) {
return 2;
}
if (Math.abs(start - to) == 2) {
return 4;
}
return 3;
}
}