์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Java

[BOJ/Step11] 1018 : ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ (JAVA)

NaNaRin๐Ÿ™ƒ 2021. 1. 21. 14:30

www.acmicpc.net/problem/1018

 

1018๋ฒˆ: ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— N๊ณผ M์ด ์ฃผ์–ด์ง„๋‹ค. N๊ณผ M์€ 8๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ณด๋“œ์˜ ๊ฐ ํ–‰์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. B๋Š” ๊ฒ€์€์ƒ‰์ด๋ฉฐ, W๋Š” ํฐ์ƒ‰์ด๋‹ค.

www.acmicpc.net


๋ฌธ์ œ

์ง€๋ฏผ์ด๋Š” ์ž์‹ ์˜ ์ €ํƒ์—์„œ MN๊ฐœ์˜ ๋‹จ์œ„ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋Š” M*N ํฌ๊ธฐ์˜ ๋ณด๋“œ๋ฅผ ์ฐพ์•˜๋‹ค. ์–ด๋–ค ์ •์‚ฌ๊ฐํ˜•์€ ๊ฒ€์€์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ๊ณ , ๋‚˜๋จธ์ง€๋Š” ํฐ์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ๋‹ค. ์ง€๋ฏผ์ด๋Š” ์ด ๋ณด๋“œ๋ฅผ ์ž˜๋ผ์„œ 8*8 ํฌ๊ธฐ์˜ ์ฒด์ŠคํŒ์œผ๋กœ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค.

์ฒด์ŠคํŒ์€ ๊ฒ€์€์ƒ‰๊ณผ ํฐ์ƒ‰์ด ๋ฒˆ๊ฐˆ์•„์„œ ์น ํ•ด์ ธ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๊ตฌ์ฒด์ ์œผ๋กœ, ๊ฐ ์นธ์ด ๊ฒ€์€์ƒ‰๊ณผ ํฐ์ƒ‰ ์ค‘ ํ•˜๋‚˜๋กœ ์ƒ‰์น ๋˜์–ด ์žˆ๊ณ , ๋ณ€์„ ๊ณต์œ ํ•˜๋Š” ๋‘ ๊ฐœ์˜ ์‚ฌ๊ฐํ˜•์€ ๋‹ค๋ฅธ ์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ด ์ •์˜๋ฅผ ๋”ฐ๋ฅด๋ฉด ์ฒด์ŠคํŒ์„ ์ƒ‰์น ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๋‘ ๊ฐ€์ง€๋ฟ์ด๋‹ค. ํ•˜๋‚˜๋Š” ๋งจ ์™ผ์ชฝ ์œ„ ์นธ์ด ํฐ์ƒ‰์ธ ๊ฒฝ์šฐ, ํ•˜๋‚˜๋Š” ๊ฒ€์€์ƒ‰์ธ ๊ฒฝ์šฐ์ด๋‹ค.

๋ณด๋“œ๊ฐ€ ์ฒด์ŠคํŒ์ฒ˜๋Ÿผ ์น ํ•ด์ ธ ์žˆ๋‹ค๋Š” ๋ณด์žฅ์ด ์—†์–ด์„œ, ์ง€๋ฏผ์ด๋Š” 8*8 ํฌ๊ธฐ์˜ ์ฒด์ŠคํŒ์œผ๋กœ ์ž˜๋ผ๋‚ธ ํ›„์— ๋ช‡ ๊ฐœ์˜ ์ •์‚ฌ๊ฐํ˜•์„ ๋‹ค์‹œ ์น ํ•ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. ๋‹น์—ฐํžˆ 8*8 ํฌ๊ธฐ๋Š” ์•„๋ฌด๋ฐ์„œ๋‚˜ ๊ณจ๋ผ๋„ ๋œ๋‹ค. ์ง€๋ฏผ์ด๊ฐ€ ๋‹ค์‹œ ์น ํ•ด์•ผ ํ•˜๋Š” ์ •์‚ฌ๊ฐํ˜•์˜ ์ตœ์†Œ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N๊ณผ M์ด ์ฃผ์–ด์ง„๋‹ค. N๊ณผ M์€ 8๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ณด๋“œ์˜ ๊ฐ ํ–‰์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. B๋Š” ๊ฒ€์€์ƒ‰์ด๋ฉฐ, W๋Š” ํฐ์ƒ‰์ด๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ง€๋ฏผ์ด๊ฐ€ ๋‹ค์‹œ ์น ํ•ด์•ผ ํ•˜๋Š” ์ •์‚ฌ๊ฐํ˜• ๊ฐœ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1

8 8

WBWBWBWB

BWBWBWBW

WBWBWBWB

BWBBBWBW

WBWBWBWB

BWBWBWBW

WBWBWBWB

BWBWBWBW

 

์˜ˆ์ œ ์ถœ๋ ฅ 1

1

 

์˜ˆ์ œ ์ž…๋ ฅ 2

10 13

BBBBBBBBWBWBW

BBBBBBBBBWBWB

BBBBBBBBWBWBW

BBBBBBBBBWBWB

BBBBBBBBWBWBW

BBBBBBBBBWBWB

BBBBBBBBWBWBW

BBBBBBBBBWBWB

WWWWWWWWWWBWB

WWWWWWWWWWBWB

 

์˜ˆ์ œ ์ถœ๋ ฅ 2

12


ํ’€์ด

 

์ฒด์ŠคํŒ์€ ๋งจ ์™ผ์ชฝ ์œ„ ์นธ์ด ์œ„์ชฝ์ด ๊ฒ€์ •์ƒ‰์ธ ๊ฒฝ์šฐ, ํฐ์ƒ‰์ธ ๊ฒฝ์šฐ ๋‘๊ฐ€์ง€๊ฐ€ ์กด์žฌํ•œ๋‹ค.

 

์ฒด์ŠคํŒ์˜ ํฌ๊ธฐ๊ฐ€ 8*8๋ณด๋‹ค ํด ๊ฒฝ์šฐ ์ฒด์ŠคํŒ์„ ์ž˜๋ผ 8*8 ํฌ๊ธฐ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š”๋ฐ, ์ž๋ฅธ ํ›„ ์ฒด์ŠคํŒ์ฒ˜๋Ÿผ ์ƒ‰์„ ์น ํ•  ๋•Œ ๊ฐ€์žฅ ์ ๊ฒŒ ์ •์‚ฌ๊ฐํ˜•์„ ์น ํ•˜๋Š” ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.

 

๊ฐ€์žฅ ์ ๊ฒŒ ์ •์‚ฌ๊ฐํ˜•์„ ์น ํ•˜๋ ค๋ฉด ์–ด๋Š ๋ถ€๋ถ„์„ ์ž˜๋ผ์•ผ ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—, (0, 0) ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž๋ฅผ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๊ฐ€์žฅ ์ ์€ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.

 

int board(char[][], int x, int y)

์ž…๋ ฅ๋ฐ›์€ char[][] ๋ฐฐ์—ด๊ณผ ๊ฒ€์‚ฌํ•  x, y ์ขŒํ‘œ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ•จ์ˆ˜ ์•ˆ์— ์กด์žฌํ•˜๋Š” ๋งจ ์™ผ์ชฝ ์œ„ ์นธ์ด ๊ฒ€์ •์ƒ‰์ธ ์ฒด์ŠคํŒ, ํฐ์ƒ‰์ธ ์ฒด์ŠคํŒ ๋‘ ๊ฐœ์™€ ๋น„๊ตํ•˜์—ฌ ๋‹ค๋ฅธ ๋ถ€๋ถ„์ด ๋” ์ ์€ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜

=> Math.min() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ

 

1. ์ž…๋ ฅ๋ฐ›์€ n, m ํฌ๊ธฐ์˜ char[][] myBoard 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ๋ณด๋“œ์˜ ์ƒํƒœ๋ฅผ ์ €์žฅ

2. min์„ (0, 0) ์œ„์น˜์—์„œ์˜ ํšŸ์ˆ˜๋กœ ์ดˆ๊ธฐํ™”

3. ์ด์ค‘๋ฃจํ”„๋ฅผ ํ†ตํ•ด ๋ณด๋“œ๋ฅผ ์ž๋ฅผ ์ˆ˜ ์žˆ๋Š” ์™ผ์ชฝ ์œ„ ๊ผญ์ง€์  ์ขŒํ‘œ๋ฅผ (0, 0) ๋ถ€ํ„ฐ board()๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , min๊ณผ ๋น„๊ตํ•˜์—ฌ ๋” ์ž‘์€ ํšŸ์ˆ˜๋ฅผ min์— ์ €์žฅ

4.min ์ถœ๋ ฅ

 

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

public class B1018 {
	
	static int board(char[][] myBoard, int x, int y) {
		char[][] BBoard = {
				{'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'},
		};
		char[][] WBoard = {
				{'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'},
		};
		int countB = 0;
		int countW = 0;
		
		for(int i = 0; i < 8; i++) {
			for(int j = 0; j < 8; j++) {
				if(BBoard[i][j] != myBoard[x + i][y + j]) {
					countB++;
				}
				if(WBoard[i][j] != myBoard[x + i][y + j]) {
					countW++;
				}
			}
		}
		return Math.min(countW, countB);
	}
	
	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());
		
		char[][] myBoard = new char[n][m];
	
		for(int i = 0; i < n; i++) {
			String s = br.readLine();
			for(int j = 0; j < m; j++) {
				myBoard[i][j] = s.charAt(j);
			}
		}
		
		int min = board(myBoard, 0, 0);
		
		for(int i = 0; i <= n - 8; i++) {
			for(int j = 0; j <= m - 8; j++) {
				min = min < board(myBoard, i, j) ? min : board(myBoard, i, j);
			}
		}	
		System.out.println(min);
	}
}