Til7
title: 2025-09-04 author: 강병호 date: 2025-08-04 category: TIL/강병호/2025/08 layout: post —
package Q1018;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Q1018 {
public static void main(String[] args) throws Exception {
BufferedReapackage Q1018;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Q1018 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
char[][] map = new char[N][M];
for (int i = 0; i < N; i++) {
String line = br.readLine();
map[i] = line.toCharArray();
}
int minCount = 64;
for (int y = 0; y <= N - 8; y++) {
for (int x = 0; x <= M - 8; x++) {
int countW = 0;
for (int i = y; i < y + 8; i++) {
for (int j = x; j < x + 8; j++) {
char expectedChar;
if ((i + j) % 2 == 0) {
expectedChar = 'W';
} else {
expectedChar = 'B';
}
if (map[i][j] != expectedChar) {
countW++;
}
}
}
int countB = 0;
for (int i = y; i < y + 8; i++) {
for (int j = x; j < x + 8; j++) {
char expectedChar;
if ((i + j) % 2 == 0) {
expectedChar = 'B';
} else {
expectedChar = 'W';
}
if (map[i][j] != expectedChar) {
countB++;
}
}
}
int min = Math.min(countW, countB);
if (min < minCount) {
minCount = min;
}
}
}
System.out.println(minCount);
}
}
der br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
char[][] map = new char[N][M];
for (int i = 0; i < N; i++) {
String line = br.readLine();
map[i] = line.toCharArray();
}
int minCount = 64;
for (int y = 0; y <= N - 8; y++) {
for (int x = 0; x <= M - 8; x++) {
int countW = 0;
for (int i = y; i < y + 8; i++) {
for (int j = x; j < x + 8; j++) {
char expectedChar;
if ((i + j) % 2 == 0) {
expectedChar = 'W';
} else {
expectedChar = 'B';
}
if (map[i][j] != expectedChar) {
countW++;
}
}
}
int countB = 0;
for (int i = y; i < y + 8; i++) {
for (int j = x; j < x + 8; j++) {
char expectedChar;
if ((i + j) % 2 == 0) {
expectedChar = 'B';
} else {
expectedChar = 'W';
}
if (map[i][j] != expectedChar) {
countB++;
}
}
}
int min = Math.min(countW, countB);
if (min < minCount) {
minCount = min;
}
}
}
System.out.println(minCount);
}
}