풀이)
만약 주어진 NM판에서 N, M이 8보다 클 경우 그 안에 있는 88 정사각형은 (N-7)(M-7) 개이다.
때문에 우리는 (N-7)*(M-7)개의 판에서 색을 바꾸는 횟수가 가장 적은 판을 찾아내면 된다.
내 코드)
import java.io.*;
import java.util.StringTokenizer;
public class Backjoon1018 {
public static boolean[][] arr;
public static int min = 64;
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
arr = new boolean[N][M];
for (int i = 0; i < N; i++) {
String str = bf.readLine();
for (int j = 0; j < M; j++) {
if (str.charAt(j) == 'W') {
arr[i][j] = true; // W일 때는 true
} else {
arr[i][j] = false; // B일 때는 false
}
}
}
int N_row = N - 7;
int M_col = M - 7;
for (int i = 0; i < N_row; i++) {
for (int j = 0; j < M_col; j++) {
find(i, j);
}
}
System.out.println(min);
}
public static void find(int x, int y) {
int end_x = x + 8;
int end_y = y + 8;
int count = 0;
boolean TF = arr[x][y]; // 첫 번째 칸의 색
for (int i = x; i < end_x; i++) {
for (int j = y; j < end_y; j++) {
// 올바른 색이 아닐경우 count 1 증가
if (arr[i][j] != TF) {
count++;
}
TF = (!TF);
}
TF = !TF;
}
count = Math.min(count, 64 - count);
min = Math.min(min, count);
}
}