[완전탐색] 백준 1018 체스판 다시 칠하기 JAVAAlgorithm2024. 4. 9. 18:04
Table of Contents
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ES_5_1018 {
static String[][] white = {
{"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"}
};
static String[][] board;
public static void main(String[] args) throws IOException {
// [완전탐색] 백준 1018 체스판 다시 칠하기 JAVA
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]); // n : y(height)
int m = Integer.parseInt(input[1]); // m : x(width)
board = new String[m][n];
for (int i = 0 ; i < n ; i++) {
String row = br.readLine();
for (int j = 0 ; j < m ; j++) {
board[j][i] = String.valueOf(row.charAt(j));
}
}
int result = Integer.MAX_VALUE;
for (int i = 0 ; i <= n - 8 ; i++) { // n : y(height)
for (int j = 0 ; j <= m - 8 ; j++) { // m : x(width)
int temp = getChangeCnt(j, i);
result = Math.min(result, temp);
}
}
System.out.println(result);
}
private static int getChangeCnt(int x, int y) {
int result = 0;
int idxX = x;
int idxY = y;
for (int i = 0 ; i < 8 ; i++) {
for (int j = 0 ; j < 8 ; j++) {
String obj = board[idxX][idxY];
String bo = white[j][i];
if (!obj.equals(bo)) {
result++;
}
idxX++;
}
idxX = x;
idxY++;
}
result = Math.min(result, 64-result); //white = 64-black
return result;
}
}
시간복잡도 : O(n * m)
'Algorithm' 카테고리의 다른 글
| [DFS] 백준 11724 연결 요소의 개수 JAVA (0) | 2024.04.11 |
|---|---|
| [완전탐색] 백준 17484 진우의 달 여행 JAVA (0) | 2024.04.10 |
| [그리디] 백준 2217 로프 JAVA (0) | 2024.04.09 |
| [DP] 백준 1463 1로 만들기 JAVA (0) | 2024.04.09 |
| [DFS] 백준 1012 유기농배추 JAVA (0) | 2024.04.08 |