문제
BOJ 1018 체스판 다시 칠하기
풀이
- 맨 왼쪽 위 칸이 흰색인 체스판과 검은색인 체스판을 만듭니다.
- 보드 정보를 입력받습니다. N=가로길이 M=세로길이
- 입력받은 보드(N x M)에서 8 x 8 부분을 흰색 체스판, 검은색 체스판과 비교하여 수정이 필요한 타일 수를 계산합니다.
- 3번 과정을 반복하여 수정이 필요한 최소 타일 수를 구한 뒤 출력합니다.
코드
import java.io.*;
import java.util.*;
public class Q1018 {
public static final char[][] whiteBoard = {
{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
};
public static final char[][] blackBoard = {
{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
char[][] board = new char[N][M];
for (int i = 0; i < N; i++) {
String row = br.readLine();
for (int j = 0; j < M; j++) {
board[i][j] = row.charAt(j);
}
}
int editTileCount = Integer.MAX_VALUE;
for (int i = 0; i <= N - 8; i++) {
for (int j = 0; j <= M - 8; j++) {
int whiteTileCount = 0;
int blackTileCount = 0;
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
final char c = board[x + i][y + j];
if (c != whiteBoard[x][y]) {
whiteTileCount++;
}
if (c != blackBoard[x][y]) {
blackTileCount++;
}
}
}
editTileCount = Math.min(editTileCount, Math.min(whiteTileCount, blackTileCount));
}
}
bw.write(editTileCount + "\n");
bw.close();
br.close();
}
}
결과