티스토리 뷰

[BJ] 1018. 체스판 다시 칠하기

www.acmicpc.net/problem/1018

 

1018번: 체스판 다시 칠하기

첫째 줄에 N과 M이 주어진다. N과 M은 8보다 크거나 같고, 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에는 보드의 각 행의 상태가 주어진다. B는 검은색이며, W는 흰색이다.

www.acmicpc.net

import java.io.*;
import java.util.*;
// 210211

public class Main_BJ_1018_체스판다시칠하기 {
	static int N, M;
	static char[][] map;
	static int min;
	static char[][] chess1 = {
			{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
			{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
			{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
			{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
			{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
			{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
			{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
			{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'}
			
	};
	
	static char[][] chess2 = {
			{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
			{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
			{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
			{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
			{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
			{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'},
			{'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'},
			{'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'}
			
	};
	
	static void printArr(int sr, int sc) {
		for(int r=sr; r<sr+8; r++) {
			for(int c=sc; c<sc+8; c++) {
				System.out.print(map[r][c] + " ");
			}
			System.out.println();
		}
	}
	
	
	static void chess(int sr, int sc) {
		
//		printArr(sr, sc);
		
		int cnt1 = 0;
		int cnt2 = 0;
		int chessR = 0;
		int chessC = 0;
		
		for(int r=sr; r<sr+8; r++, chessR++) {
			chessC = 0;
			for (int c=sc; c<sc+8; c++, chessC++) {
				if (map[r][c] != chess1[chessR][chessC]) cnt1++;
				if (map[r][c] != chess2[chessR][chessC]) cnt2++;
			}
		}// for
		
		// cnt 크기 비교하기
		min = Math.min(min, Math.min(cnt1, cnt2));
	}
	
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("res/input_BJ_1018_체스판다시칠하기.txt"));
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		StringTokenizer st = new StringTokenizer(br.readLine(), " ");
		N = Integer.parseInt(st.nextToken());
		M = Integer.parseInt(st.nextToken());
		min = Integer.MAX_VALUE;
		
		map = new char[N][M];
		
		for(int r=0; r<N; r++) {
			map[r] = br.readLine().toCharArray();
		}
		
		// 8 * 8로 나누기
		for(int r=0; r<=N-8; r++) {
			for(int c=0; c<=M-8; c++) {
				chess(r, c);
			}
		}
		
		System.out.println(min);
		br.close();
	} // main

}

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

[BJ] 11650. 좌표 정렬하기  (0) 2021.02.14
[BJ] 1436. 영화감독 숌  (0) 2021.02.14
[BJ] 7568. 덩치  (0) 2021.02.14
[BJ] 2231. 분해합  (0) 2021.02.14
[BJ] 2798. 블랙잭  (0) 2021.02.14
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함