변수의 기록

(코테) 백준_1018_ 체스판 다시 칠하기 본문

카테고리 없음

(코테) 백준_1018_ 체스판 다시 칠하기

불광동 물주먹 2025. 5. 8. 13:24

 

 

 

 

 

package test;

import java.util.*;
import java.io.*;
/*
10 13
BBBBBBBBWBWBW
BBBBBBBBBWBWB
BBBBBBBBWBWBW
BBBBBBBBBWBWB
BBBBBBBBWBWBW
BBBBBBBBBWBWB
BBBBBBBBWBWBW
BBBBBBBBBWBWB
WWWWWWWWWWBWB
WWWWWWWWWWBWB
*/

public class Backjun_1018 {
 static char[][] board ;
 static int n;
 static int m;
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
			BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
			StringTokenizer st = new StringTokenizer(br.readLine());
			  n = Integer.parseInt(st.nextToken());
			  m = Integer.parseInt(st.nextToken());
			 board = new char[n][m];			 			
			 
		        for (int i = 0; i < n; i++) {
		            board[i] = br.readLine().toCharArray();
		        }
			 int min = Integer.MAX_VALUE;
			 
			 for(int i = 0; i <= n-8;i++) {
				 for(int j = 0; j <=m-8;j++) {
					 min = Math.min(min, start(i,j));
					 
				 }				 				 	 
			 }			 
			  System.out.println(min);			 
	}
	
	public static int start(int a , int b) {
		int countW =0;
		int countB =0;
		
		for(int i = a ; i<a+8 ;i++) {
			for(int j = b ; j<b+8; j++) {				
				boolean flag = (i+j) % 2==0;				
				if(board[i][j] != (flag? 'W' :'B')) countW++ ;
				if(board[i][j] != (flag?  'B' : 'W')) countB++ ;
			}			
		}		
		return Math.min(countW, countB);
	}

}

 

 

회고. 

1.new char[]   , br.readLine.toCharArray();