백준

백준 1018번 문제 체스판 다시 칠하기 (Java)

gxxgsta 2021. 7. 15. 22:44
반응형
SMALL
import java.util.Scanner;

public class Main{
	
	 public static boolean[][] arr;
	 public static int min = 64;
	 
    public static void main(String args[]){
    	Scanner sc = new Scanner(System.in);
    	
    	int N = sc.nextInt();
    	int M = sc.nextInt();
    	sc.nextLine();
    	
    	arr = new boolean[N][M];
    	
    	for(int i=0; i<N; i++) {
    		String str = sc.nextLine().trim();
    		for(int j=0; j<M; j++) {
    			if(str.charAt(j) == 'W') {
    				arr[i][j] = true;
    			}else { arr[i][j] = false; }
    		}
    	}
    	
    	int row = N-7;
    	int col = M-7;
    	
    	for(int i=0; i<row; i++) {
    		for(int j=0; j<col; j++) {
    			answer(i, j);
    		}
    	}
    	
    	System.out.println(min);
    	
    }


    public static void answer(int n, int m) {
    	int row = n+8;
    	int col = m+8;
    	int count = 0;
    	boolean MH = arr[n][m];
    	
    	for(int i=n; i<row; i++) {
    		for(int j=m; j<col; j++) {
    			if(arr[i][j] != MH) { count++; }
    			MH = !MH;
    		} MH = !MH;
    	}
    	count = Math.min(count, 64 - count);
    	min = Math.min(min, count);
    }
}
반응형
LIST