Skip to content

Commit b5fd68d

Browse files
authored
[20260228] BOJ / G4 / 체스판 다시 칠하기 2 / 이준희
1 parent aa64b5d commit b5fd68d

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static int N, M, K;
7+
static char[][] board;
8+
9+
public static void main(String[] args) throws IOException {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
StringTokenizer st = new StringTokenizer(br.readLine());
12+
13+
N = Integer.parseInt(st.nextToken());
14+
M = Integer.parseInt(st.nextToken());
15+
K = Integer.parseInt(st.nextToken());
16+
17+
board = new char[N + 1][M + 1];
18+
for (int i = 1; i <= N; i++) {
19+
String line = br.readLine();
20+
for (int j = 1; j <= M; j++) {
21+
board[i][j] = line.charAt(j - 1);
22+
}
23+
}
24+
25+
System.out.println(Math.min(coloring('B'), coloring('W')));
26+
}
27+
28+
static int coloring(char start) {
29+
int[][] sum = new int[N + 1][M + 1];
30+
31+
for (int i = 1; i <= N; i++) {
32+
for (int j = 1; j <= M; j++) {
33+
int value = 0;
34+
if ((i + j) % 2 == 0) {
35+
if (board[i][j] != start) value = 1;
36+
} else {
37+
if (board[i][j] == start) value = 1;
38+
}
39+
40+
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + value;
41+
}
42+
}
43+
44+
int painting = Integer.MAX_VALUE;
45+
for (int i = K; i <= N; i++) {
46+
for (int j = K; j <= M; j++) {
47+
int count = sum[i][j] - sum[i - K][j] - sum[i][j - K] + sum[i - K][j - K];
48+
painting = Math.min(painting, count);
49+
}
50+
}
51+
return painting;
52+
}
53+
}
54+
```

0 commit comments

Comments
 (0)