본문 바로가기
항해

[DAY47] 99클럽 코딩테스트 JAVA 백준 빙산 2573번

by neVerThe1ess 2025. 2. 3.

1018번: 체스판 다시 칠하기

 

 

🌕 최종코드

import java.io.*;

public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		String NM_str = br.readLine();
		int N = Integer.parseInt(NM_str.split(" ")[0]);
		int M = Integer.parseInt(NM_str.split(" ")[1]);
		
		String[][] arr = new String[N][M];
		
		for(int i = 0 ; i < N ; i++) {
			String str = br.readLine();
			
			for(int j = 0 ; j < M ; j++) {
				if(str.charAt(j) == 'W') {
					arr[i][j] = "W";
				}else {
					arr[i][j] = "B";
				}
			}
		}
		
		int min = 64;
		
		for(int i = 0 ; i < N - 7 ; i++) { // 세로의 경우
			for(int j = 0 ; j < M - 7 ; j++) { // 가로의 경우
				min = Math.min(min, cal(i, j, arr)); 
			}
		}
		
		bw.write(String.valueOf(min));
	
		br.close();
		bw.flush();
		bw.close();
	}
	
	public static int cal(int x, int y, String[][] WB) {
		
		int count = 0;
		
		String color = "W"; 
		
		for(int i = x ; i < x + 8 ; i++) {
			for(int j = y ; j < y + 8 ; j++) { 
			
				if(!WB[i][j].equals(color)) {
					count++;
				}
				
				if(color.equals("W")) {
					color = "B";
				}else {
					color = "W";
				}
			}
			
			if(color.equals("W")) {
				color = "B";
			}else {
				color = "W";
			}
		}
		
		count = Math.min(count, 64 - count);
		
		return count;
		
	}
}