OJ
[BOJ] 1018 체스판 다시 칠하기 (JAVA)
P3PP4
2023. 4. 15. 10:00
https://www.acmicpc.net/problem/1018
1018번: 체스판 다시 칠하기
첫째 줄에 N과 M이 주어진다. N과 M은 8보다 크거나 같고, 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에는 보드의 각 행의 상태가 주어진다. B는 검은색이며, W는 흰색이다.
www.acmicpc.net
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int N, M, min = Integer.MAX_VALUE;
static String[] chess, input;
public static void main(String[] args) throws Exception {
chess = new String[2];
chess[0] = "BWBWBWBW";
chess[1] = "WBWBWBWB";
st = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
input = new String[N];
for (int i = 0; i < N; i++) {
input[i] = br.readLine();
}
for (int i = 0; i <= N - 8; i++) {
for (int j = 0; j <= M - 8; j++) {
min = Math.min(min, check(i, j));
}
}
System.out.print(min);
}
static int check(int i, int j) {
int[] result = new int[2];
int what = 0;
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++) {
if (input[i + r].charAt(j + c) != chess[what].charAt(c)) result[0]++;
if (input[i + r].charAt(j + c) != chess[(what + 1) % 2].charAt(c)) result[1]++;
}
what = (what + 1) % 2;
}
return Math.min(result[0], result[1]);
}
}