[백준 1018] 체스판 다시 칠하기
Algorithm/백준(BOJ)

[백준 1018] 체스판 다시 칠하기

반응형

[백준 1018] 체스판 다시 칠하기


문제 출처 : https://www.acmicpc.net/problem/1018






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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.Scanner;
 
public class Problem1018 {
    
    public static void main(String[] args) {
        
        Scanner s = new Scanner(System.in);
        
        int N = s.nextInt(); //행
        int M = s.nextInt(); //열
        
        int[][] c = new int[N][M]; // 체스판
        String[] str = new String[N]; //한줄씩 저장
        char[][] save = new char[N][M];
        
        for (int i = 0; i < N; i++) {
            str[i] = s.next();
        }
        
        for (int i = 0; i < save.length; i++) {
            for (int j = 0; j < save[i].length; j++) {
                save[i][j] = str[i].charAt(j);
            }
        }
        
        int min = 10000;
        int result = 0;
        
        for (int i = 0; i < N-7; i++) {
            for (int j = 0; j < M-7; j++) {
                
                int cnt1 = 0;
                int cnt2 = 0;
                
                for (int x = i, m=0; x < 8+i; x++, m++) {
                    for (int y = j, n=0; y < 8+j; y++, n++) {
                        char cp = ((x+y)%2 == 0) ? 'W' : 'B';
                        if(save[x][y] != cp) cnt1++;
                        else cnt2++;
                    }
                }
                
                if(cnt1 > cnt2) result = cnt2;
                else result = cnt1;
                
                if(min > result) min = result;
            }
        }
        
        System.out.println(min);
        
    }
 
}
 
cs


반응형

'Algorithm > 백준(BOJ)' 카테고리의 다른 글

[백준 1182] 부분집합의 합  (1) 2019.01.17
[백준 1260] DFS와 BFS - 인접 리스트 이용  (0) 2019.01.11
[백준 2805] 나무 자르기  (0) 2018.06.09
[백준 1931] 회의실 배정  (6) 2018.06.02
[백준 11047] 동전 0  (0) 2018.06.01