Skip to content

Commit cfe0c2b

Browse files
committed
[20251126] BOJ / G4 / 우유 도시 / 김민진
1 parent 9ef62cf commit cfe0c2b

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
```java
2+
import java.io.*;
3+
import java.util.StringTokenizer;
4+
5+
public class BJ_14722_우유_도시 {
6+
7+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
private static StringTokenizer st;
10+
11+
private static int N;
12+
private static int[][] milk;
13+
private static Milk[][] dp;
14+
15+
private static class Milk {
16+
int prev;
17+
int cnt;
18+
19+
Milk(int prev, int cnt) {
20+
this.prev = prev;
21+
this.cnt = cnt;
22+
}
23+
}
24+
25+
public static void main(String[] args) throws IOException {
26+
init();
27+
sol();
28+
}
29+
30+
private static void init() throws IOException {
31+
N = Integer.parseInt(br.readLine());
32+
33+
milk = new int[N + 1][N + 1];
34+
dp = new Milk[N + 1][N + 1];
35+
36+
for (int i = 1; i <= N; i++) {
37+
st = new StringTokenizer(br.readLine());
38+
for (int j = 1; j <= N; j++) {
39+
milk[i][j] = Integer.parseInt(st.nextToken());
40+
}
41+
}
42+
43+
for (int i = 0; i <= N; i++) {
44+
for (int j = 0; j <= N; j++) {
45+
dp[i][j] = new Milk(0, 0);
46+
}
47+
}
48+
}
49+
50+
private static void sol() throws IOException {
51+
for (int i = 1; i <= N; i++) {
52+
for (int j = 1; j <= N; j++) {
53+
Milk fromUp = dp[i - 1][j];
54+
Milk fromLeft = dp[i][j - 1];
55+
Milk better = fromUp.cnt >= fromLeft.cnt ? fromUp : fromLeft;
56+
57+
if (milk[i][j] == better.prev) {
58+
dp[i][j] = new Milk((better.prev + 1) % 3, better.cnt + 1);
59+
} else {
60+
dp[i][j] = new Milk(better.prev, better.cnt);
61+
}
62+
}
63+
}
64+
65+
bw.write(dp[N][N].cnt + "");
66+
bw.flush();
67+
bw.close();
68+
br.close();
69+
}
70+
}
71+
```

0 commit comments

Comments
 (0)