
소스코드:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
static BufferedReader br;
static BufferedWriter bw;
static int N;
static int M;
static char[][] board;
public static int solve(int x, int y) {
int count = 0;
boolean isWhite = (board[y][x] == 'W');
for (int yy = 0; yy < 8; yy++) {
isWhite = !isWhite;
for (int xx = 0; xx < 8; xx++) {
if (isWhite == (board[y + yy][x + xx] == 'W')) {
count++;
}
isWhite = !isWhite;
}
}
return Math.min(8 * 8 - count, count);
}
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
board = new char[N][M];
for (int i = 0; i < N; i++) {
board[i] = br.readLine().toCharArray();
}
br.close();
int min = Integer.MAX_VALUE;
for (int y = 0; y <= N - 8; y++) {
for (int x = 0; x <= M - 8; x++) {
int cur = solve(x, y);
if (cur < min) {
min = cur;
}
}
}
bw.write(min + "\n");
bw.flush();
bw.close();
}
}
글의 내용 중 잘못된 점이나 수정이 필요한 부분, 혹은 궁금한 사항이 있다면 언제든 댓글로 남겨주시면 감사하겠습니다.
여러분의 피드백은 더 나은 글을 작성하는 데 큰 도움이 됩니다. 감사합니다.
'알고리즘 > 백준' 카테고리의 다른 글
| [JAVA-자바] 2164번: 카드2 (0) | 2025.02.28 |
|---|---|
| [JAVA-자바] 1920번: 수 찾기 (0) | 2025.02.27 |
| [JAVA-자바] 11651번: 좌표 정렬하기 2 (0) | 2025.02.27 |
| [JAVA-자바] 10814번: 나이순 정렬 (0) | 2025.02.27 |
| [JAVA-자바] 7568번: 덩치 (0) | 2025.02.27 |