코드
public class 백준1018_체스판다시칠하기 {
// 1이 W, 0이 B
static int[][][] arr = {
{
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1}
},
{
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 0, 1, 0}
}
};
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
// n 이 높이
String[] input = bufferedReader.readLine().split(" ");
int n = Integer.parseInt(input[0]);
int m = Integer.parseInt(input[1]);
int inputArr[][] = new int[n][m];
for (int i = 0; i < n; i++){
String line = bufferedReader.readLine();
for (int j = 0; j < m; j++){
switch (line.charAt(j)){
case 'B':
inputArr[i][j] = 0;
break;
case 'W':
inputArr[i][j] = 1;
break;
}
}
}
int result = Integer.MAX_VALUE;
for (int i = 0; i < n-7; i++){
for (int j =0; j< m-7; j++){
for (int k = 0; k <=1; k++){
int temp = countDiff(inputArr, k, i, j);
result = Math.min(temp,result);
}
}
}
System.out.println(result);
}
private static int countDiff(int [][] inputArr, int mode, int startX, int startY){
int result = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (inputArr[startX + i][startY + j] != arr[mode][i][j]) {
result++;
}
}
}
return result;
}
}
풀이
문제를 보자마자 직관적으로 떠오른 걸 그대로 코드로 옮겼다.
먼저, 문제에 나와있는대로 체스판에 색칠하는 경우의 수는 두 가지이다.
이를 노가다로 전역 변수 arr로 미리 만들어두었다.
그리고 8x8 액자를 입력으로 받은 배열를 하나 씩 옮기는 방식으로 진행한다.
이때, 액자를 이제 기존에 만든 두 가지 경우인 전역 변수 arr과 비교한다.
mode는 그 두 가지 경우의 수를 임의로 구하기 위한 것이다.
시작점이 필요하기 때문에, startX와 startY를 매개변수로 받아 진행한다.
최소값을 구하기 위해, Math.min으로 최소를 찾는다.
그러면 최종적으로 결과값을 도출할 수 있다.
'Algorithm > 백준' 카테고리의 다른 글
[백준]9012 - 괄호 문제 풀이(Java,자바) (0) | 2024.12.29 |
---|---|
[백준]1920 - 수 찾기 문제 풀이(Java,자바) (1) | 2024.12.29 |
[백준]10814- 나이순 정렬 문제 풀이(Java,자바) (1) | 2024.12.28 |
[백준]2751 - 수 정렬하기 2 문제 풀이(Java,자바) (0) | 2024.12.28 |
[백준]1676 - 팩토리얼 0의 개수 문제 풀이(Java,자바) (0) | 2024.12.28 |