완전 탐색
import java.util.Scanner;
class Main {
public static int getSolution(int startRow, int startCol, String[] board) {
String[] orgBoard = { "WBWBWBWB", "BWBWBWBW" };
int whiteSol = 0;
for (int i = 0; i < 8; i++) {
int row = startRow + i;
for (int j = 0; j < 8; j++) {
int col = startCol + j;
if (board[row].charAt(col) != orgBoard[row % 2].charAt(j)) whiteSol++;
}
}
return Math.min(whiteSol, 64 - whiteSol);
}
public static void main(String[] args) {
// 0. input 받기
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
int col = sc.nextInt();
sc.nextLine();
String[] board = new String[row];
for (int i = 0; i < row; i++) board[i] = sc.nextLine();
// 1. 체스판 자르기
int sol = Integer.MAX_VALUE;
for (int i = 0; i <= row - 8; i++) {
for (int j = 0; j <= col - 8; j++) {
// 2. 현 체스판의 최소 비용 구하기
int curSol = getSolution(i, j, board);
// 3. 전체 최적의 값과 비교하여 갱신하기
if (sol > curSol) sol = curSol;
}
}
System.out.println(sol);
sc.close();
}
}
참고 : 개발자로 취직하기 유튜브
'solve' 카테고리의 다른 글
| [프로그래머스] Lv2. 카펫 (자바) (0) | 2024.01.09 |
|---|---|
| [프로그래머스] Lv2. 피로도 (자바) (0) | 2024.01.09 |
| [백준 2606] 바이러스 (자바) (1) | 2024.01.09 |
| [백준 2667/실버1] 단지 번호 붙이기 (자바) (0) | 2024.01.09 |
| 두번째 코딩테스트 벼락치기(또다시..) (0) | 2024.01.08 |