|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.StringTokenizer; |
| 4 | + |
| 5 | +public class BJ_25682_체스판_다시_칠하기_2 { |
| 6 | + |
| 7 | + private static final boolean BLACK = true; |
| 8 | + private static final boolean WHITE = false; |
| 9 | + |
| 10 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 12 | + private static StringTokenizer st; |
| 13 | + |
| 14 | + private static int N, M, K; |
| 15 | + |
| 16 | + private static boolean[][] board; |
| 17 | + private static int[][] prefix; |
| 18 | + |
| 19 | + public static void main(String[] args) throws IOException { |
| 20 | + init(); |
| 21 | + sol(); |
| 22 | + } |
| 23 | + |
| 24 | + private static void init() throws IOException { |
| 25 | + st = new StringTokenizer(br.readLine()); |
| 26 | + N = Integer.parseInt(st.nextToken()); |
| 27 | + M = Integer.parseInt(st.nextToken()); |
| 28 | + K = Integer.parseInt(st.nextToken()); |
| 29 | + |
| 30 | + board = new boolean[N + 1][M + 1]; |
| 31 | + prefix = new int[N + 1][M + 1]; |
| 32 | + |
| 33 | + for (int i = 1; i <= N; i++) { |
| 34 | + String s = br.readLine(); |
| 35 | + for (int j = 1; j <= M; j++) { |
| 36 | + board[i][j] = s.charAt(j - 1) == 'B' ? BLACK : WHITE; |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private static void sol() throws IOException { |
| 42 | + bw.write(Math.min(setPrefix(BLACK), setPrefix(WHITE)) + "\n"); |
| 43 | + bw.flush(); |
| 44 | + bw.close(); |
| 45 | + br.close(); |
| 46 | + } |
| 47 | + |
| 48 | + // 열 + 행이 짝수일 때 color라고 가정 |
| 49 | + private static int setPrefix(boolean color) { |
| 50 | + int val; |
| 51 | + |
| 52 | + for (int i = 1; i <= N; i++) { |
| 53 | + for (int j = 1; j <= M; j++) { |
| 54 | + if ((i + j) % 2 == 0) { |
| 55 | + val = board[i][j] != color ? 1 : 0; |
| 56 | + } else { |
| 57 | + val = board[i][j] == color ? 1 : 0; |
| 58 | + } |
| 59 | + prefix[i][j] = prefix[i][j - 1] + prefix[i - 1][j] - prefix[i - 1][j - 1] + val; |
| 60 | + } |
| 61 | + } |
| 62 | + return getMinPrefix(); |
| 63 | + } |
| 64 | + |
| 65 | + private static int getMinPrefix() { |
| 66 | + int cnt = Integer.MAX_VALUE; |
| 67 | + |
| 68 | + for (int i = K; i <= N; i++) { |
| 69 | + for (int j = K; j <= M; j++) { |
| 70 | + cnt = Math.min(cnt, prefix[i][j] - prefix[i - K][j] - prefix[i][j - K] + prefix[i - K][j - K]); |
| 71 | + } |
| 72 | + } |
| 73 | + return cnt; |
| 74 | + } |
| 75 | + |
| 76 | +} |
| 77 | +``` |
0 commit comments