Algorithm 문제풀이

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

cjsong 2019. 6. 4. 01:01

시뮬레이션 문제

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
56
57
58
59
60
61
62
63
64
65
 
public class Main{
    static String[] WStart = { "WBWBWBWB""BWBWBWBW""WBWBWBWB""BWBWBWBW""WBWBWBWB""BWBWBWBW""WBWBWBWB",
            "BWBWBWBW" };
    static String[] BStart = { "BWBWBWBW""WBWBWBWB""BWBWBWBW""WBWBWBWB""BWBWBWBW""WBWBWBWB""BWBWBWBW",
            "WBWBWBWB" };
    private static char[][] chess;
 
    public static void main(String[] args) throws Exception {
        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());
        chess = new char[N + 1][M + 1];
        for (int i = 0; i < N; i++) {
            String str = br.readLine();
            for (int j = 0; j < M; j++) {
                chess[i][j] = str.charAt(j);
            }
        }
 
        int minNum = Integer.MAX_VALUE;
        for (int i = 0; i < N - 7; i++) {
            for (int j = 0; j < M - 7; j++) {
                minNum= min(minNum, Wcheck(WStart, i, j), Bcheck(BStart, i, j));
            }
        }
        System.out.println(minNum);
 
    }// end of main
    
    public static int Wcheck(String[] WStart, int x,  int y) {
        int result= 0 ;
        for (int i = x; i < x+8; i++) {
            for (int j = y; j < y+8; j++) {
                if(chess[i][j] == WStart[i-x].charAt(j-y)) result++;
            }
        }
        
        
        return result;
    }
    
    public static int Bcheck(String[] BStart , int x , int y) {
        int result= 0 ;
        for (int i = x; i < x+8; i++) {
            for (int j = y; j < y+8; j++) {
                if(chess[i][j] == BStart[i-x].charAt(j-y)) result++;
            }
        }
        
        
        return result;
    }
    
    public static int min(int a, int b, int c) {
        int result= 0;
        result = a < b ? (a < c ? a : c) : (b < c ? b : c);
        return result;
    }
}// end of class
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs