코딩테스트

백준 1018번 : 체스판 다시 칠하기

joonwoong 2024. 2. 21. 16:51

풀이 순서

1. BufferedReader 사용하여 입력받고

2. 8*8로 자르기

3. 첫번째 칸 색을 기준으로 비교해주기

4. 칸의 색이 다르게 나온 경우 count 값 증가시키기

5.  나올 수 있는 8*8 배열들과 다 비교하여 최솟값 구하기

 

 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.IOException;
 
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') {
                    // W일 때는 true 
					arr[i][j] = true;		
				} else {
                    // B일 때는 false
					arr[i][j] = 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++) {
				find(i, j);
			}
		}
		System.out.println(min);
	}
 
	
	public static void find(int x, int y) {
		int end_x = x + 8;
		int end_y = y + 8;
		int count = 0;
 
		boolean TF = arr[x][y];	
 
		for (int i = x; i < end_x; i++) {
			for (int j = y; j < end_y; j++) {
				if (arr[i][j] != TF) {	
					count++;
				}
				TF = (!TF);
			}
			TF = !TF;
		}
		count = Math.min(count, 64 - count);
		min = Math.min(min, count);
	}
}

'코딩테스트' 카테고리의 다른 글

SWEA 1213 : String  (0) 2024.03.05
SWEA 2005 : 파스칼의 삼각형  (0) 2024.02.23
SWEA 2072 : 간단한 369게임  (0) 2024.02.22
SWEA 1859 : 백만장자 프로젝트  (0) 2024.02.22
백준 2751 : 수 정렬하기 2  (0) 2024.02.21