본문 바로가기

Problem Solving/Baekjoon

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

1018번 체스판 다시 칠하기

문제 보러가기

🅰 설계

입력받은 보드를 8*8로 잘라서, WBWB... 형식이나 BWBW...형식의 체스판으로 만드는데 최소의 비용이 드는 지점을 찾으면 된다.

이는 단순히 8*8짜리 WBWB... , BWBW... 형식과 비교할 수도 있지만, W를 0로, B를 1으로 생각하면

BWBW...와 맞는 체스판은 ((i+j)%2)^(c == 'W' ? 1 : 0)으로,

WBWB...와 맞는 체스판은 ((i+j)%2)^(c == 'B' ? 1 : 0)으로 계산할 수 있다.

이를 보드의 모든 8*8 격자에 체크하면 된다.

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
import java.io.*;
import java.util.*;
 
public class 체스판다시칠하기 {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
 
    static String[] board = new String[50];
    public static void main(String[] args) throws IOException{
        int ans = Integer.MAX_VALUE;
        int n,m;
        st = new StringTokenizer(br.readLine());
        n = Integer.parseInt(st.nextToken());
        m = Integer.parseInt(st.nextToken());
        for(int i=0;i<n;i++) {
            board[i] = br.readLine();
        }
 
        for(int i=0;i<=n-8;i++){
            for(int j=0;j<=m-8;j++){
                ans = Math.min(ans,calc(i,j));
            }
        }
        System.out.print(ans);
    }
 
    static private int calc(int y,int x){
        int subans1 = 0, subans2 = 0;
        for(int i=0;i<8;i++){
            for(int j=0;j<8;j++){
                char c = board[y+i].charAt(x+j);
                subans1 += ((i+j)%2)^(c == 'W' ? 1 : 0);
                subans2 += ((i+j)%2)^(c == 'B' ? 1 : 0);
            }
        }
        return Math.min(subans1,subans2);
    }
}
cs

 

✅ 후기

오랜만에 다시 비트를 사용했다. 그치만 단순한 솔루션이 먼저 생각나면 굳이 돌아갈 필요 없이 바로 생각나는걸 사용하자. 멋지게, 화려하게 풀 필요가 없다.