반응형
📖 문제
📃 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static boolean[][] arr;
static int min = 64;
public static void main(String[] args) throws IOException {
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());
arr = new boolean[N][M];
// W = true, B = false
for (int i = 0; i < N; i++) {
String str = br.readLine();
for (int j = 0; j < M; j++) {
if (str.charAt(j) == 'W') {
arr[i][j] = true;
} else {
arr[i][j] = false;
}
}
}
// check the number of cases
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);
}
private static void find(int x, int y) {
int end_x = x + 8;
int end_y = y + 8;
int cnt = 0;
// first color of chess table
boolean TF = arr[x][y];
// start x ~ x+8 for find everything
for (int i = x; i < end_x; i++) {
for (int j = y; j < end_y; j++) {
if (arr[i][j] != TF) {
cnt++;
}
// change color for searching
TF = !TF;
}
// change color for searching
TF = !TF;
}
cnt = Math.min(cnt, 64 - cnt);
min = Math.min(min, cnt);
}
}
🔗 링크
https://www.acmicpc.net/problem/1018
'Study & Project ✏️ > 알고리즘 📋' 카테고리의 다른 글
백준[JAVA] 2164.카드2 - 자바 (0) | 2022.10.20 |
---|---|
백준[JAVA] 10773.제로 - 자바 (0) | 2022.10.20 |
백준[JAVA] 10845.큐 - 자바 (0) | 2022.10.17 |
백준[JAVA] 4153.직각삼각형- 자바 (0) | 2022.10.17 |
백준[JAVA] 1436.영화감독 숌- 자바 (0) | 2022.10.17 |