백준 1018번(Java)

박은지·2025년 2월 5일
0

백준

목록 보기
23/89
post-thumbnail

import java.io.*;
import java.util.*;

public class Main {
	public static boolean[][] arr;
	public static int min = 64;

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine()," ");
		
		int N = Integer.parseInt(st.nextToken());
		int M = Integer.parseInt(st.nextToken());
		
		arr = new boolean[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] = true; // W면 true
				} else {
					arr[i][j] = false; // B면 false 라고 가정
				}
			}
		}
		
		int N_row = N-7;
		int M_col = M-7;
		
		for(int i=0; i<N_row; i++) {
			for(int j=0; j<M_col; j++) {
				search(i,j);
			}
		}
		System.out.println(min);
	}
	
	public static void search(int x, int y) {
		int end_x = x+8;
		int end_y = y+8;
		int count = 0;
		
		boolean result = arr[x][y];
		
		for(int i=x; i<end_x; i++) {
			for(int j=y; j<end_y; j++) {
				if(arr[i][j] != result) {
					count++;
				}
				// 다음칸 색이 다르기 때문에 true면 false, false면 true로 변경
				result = (!result);
			}
			result = !result;
		}
		// 첫번째 칸을 기준으로 색칠할 count와 첫번째 칸 반대 색을 기준으로 할때의 64-count중 최소값 저장
		count = Math.min(count, 64-count);
		
		// 이전까지 최소값보다 현재 count가 더 작으면 min 수정
		min = Math.min(min, count);
	}
}
profile
백엔드 개발자가 되고싶은 eunzi😊

0개의 댓글