Notice
Recent Posts
Recent Comments
05-21 07:17
«   2024/05   »
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
Archives
Today
Total
관리 메뉴

Byeol Lo

N25682 - 체스판 다시 칠하기 2 본문

Algorithm/Accumulated Sum

N25682 - 체스판 다시 칠하기 2

알 수 없는 사용자 2024. 2. 13. 14:17
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.StringTokenizer;


public class Main {
    static final BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;

    public static void main(String[] args) throws IOException {
        String tmp;
        st = new StringTokenizer(BR.readLine());
        int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken()), k = Integer.parseInt(st.nextToken());
        int paint1 = 4000000, paint2 = 4000000;
        int[][] case1 = new int[n+1][m+1], case2 = new int[n+1][m+1];

        boolean[][] base = new boolean[2][2];

        base[0] = new boolean[] {true, false};
        base[1] = new boolean[] {false, true};

        for (int i = 0; i < n; i++) {
            tmp = BR.readLine();
            for (int j = 0; j < m; j++) {
                case1[i+1][j+1] = case1[i+1][j] + case1[i][j+1] - case1[i][j] +
                        (tmp.charAt(j) == 'B' ^ base[i % 2][j % 2] ? 1:0);
                case2[i+1][j+1] = case2[i+1][j] + case2[i][j+1] - case2[i][j] +
                        (tmp.charAt(j) == 'W' ^ base[i % 2][j % 2] ? 1:0);
            }
        }

        for (int i = 0; i < n-k+1; i++) {
            for (int j = 0; j < m-k+1; j++) {
                paint1 = Math.min(paint1, case1[i+k][j+k] - case1[i][j+k] - case1[i+k][j] + case1[i][j]);
                paint2 = Math.min(paint2, case2[i+k][j+k] - case2[i][j+k] - case2[i+k][j] + case2[i][j]);
            }
        }
        System.out.println(Math.min(paint1, paint2));
    }
}

'Algorithm > Accumulated Sum' 카테고리의 다른 글

N11660 - 구간 합 구하기 5  (1) 2024.02.13
N10986 - 나머지 합  (0) 2024.02.13
Comments