|
| 1 | +package backjoon; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.StringTokenizer; |
| 7 | + |
| 8 | +public class _1018 { |
| 9 | + public static boolean[][] arr; |
| 10 | + public static int min = 64; //8*8ํ์ด๋ฏ๋ก ๊ฐ์ฅ ๋ง์ด ์น ํ ์ ์๋ ์ต๋๊ฐ |
| 11 | + |
| 12 | + public static void main(String[] args) throws IOException { |
| 13 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 14 | + // memory 11484 runtime 88 |
| 15 | + StringTokenizer st = new StringTokenizer(br.readLine(), " "); |
| 16 | + |
| 17 | + int N = Integer.parseInt(st.nextToken()); |
| 18 | + int M = Integer.parseInt(st.nextToken()); |
| 19 | + |
| 20 | + arr = new boolean[N][M]; |
| 21 | + |
| 22 | + // ๋ฐฐ์ด ์์ฑ |
| 23 | + for (int i = 0; i < N; i++) { |
| 24 | + String str = br.readLine(); |
| 25 | + |
| 26 | + for (int j = 0; j < M; j++) { |
| 27 | + // ํฐ์์ผ๋ true |
| 28 | + if (str.charAt(j) == 'W') { |
| 29 | + arr[i][j] = true; |
| 30 | + } |
| 31 | + // ๊ฒ์ ์์ผ๋ false |
| 32 | + else { |
| 33 | + arr[i][j] = false; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // ๋์งธ์ค๋ถํฐ ์์ |
| 39 | + for (int i = 0; i < N - 7; i++) { |
| 40 | + for (int j = 0; j < M - 7; j++) { |
| 41 | + findMin(i, j); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + System.out.println(min); |
| 46 | + } |
| 47 | + |
| 48 | + public static void findMin(int x, int y) { |
| 49 | + int count = 0; |
| 50 | + boolean color_yn = arr[x][y]; // ์ฒซ ๋ฒ์งธ ์นธ์ ์ |
| 51 | + |
| 52 | + for (int i = x; i < x + 8; i++) { |
| 53 | + for (int j = y; j < y + 8; j++) { |
| 54 | + |
| 55 | + // ์ฌ๋ฐ๋ฅธ ์์ด ์๋๊ฒฝ์ฐ ์์ ๋ฐ๊ฟ์ค์ผํ๋ฏ๋ก count 1 ์ฆ๊ฐ |
| 56 | + if (arr[i][j] != color_yn) { |
| 57 | + count++; |
| 58 | + } |
| 59 | + // ๋ค์ ์นธ์ ๋ฐ๋์์ผ๋ก ๋ณ๊ฒฝ |
| 60 | + color_yn = (!color_yn); |
| 61 | + } |
| 62 | + color_yn = !color_yn; |
| 63 | + } |
| 64 | + |
| 65 | + count = Math.min(count, 64 - count); |
| 66 | + min = Math.min(min, count); |
| 67 | + } |
| 68 | +} |
0 commit comments