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

chaen-ing·2024년 3월 25일
0

1일1백준

목록 보기
6/18

https://www.acmicpc.net/problem/1018

package boj1018;

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

public class Main {
    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 [][] chess = new char[N][M];
        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 cnt = 0;
        int min = Integer.MAX_VALUE;
        int minW;
        int minB;

        for(int nStart = 0; nStart < N-7; nStart++){
            for(int mStart = 0; mStart < M-7; mStart++){
                minW = 0;
                cnt = 0;
                // W로 시작하는 경우
                for(int i = nStart; i < nStart + 8; i++){
                    for(int j = mStart; j < mStart + 8; j++){
                        if(cnt % 2 == 0){
                            if(chess[i][j] != 'W') minW++;
                        }else{
                            if(chess[i][j] != 'B') minW++;
                        }
                        cnt++;
                    }
                    cnt--;
                }
                // B로 시작하는 경우
                minB = 0;
                cnt = 0;
                for(int i = nStart; i < nStart + 8; i++){
                    for(int j = mStart; j < mStart + 8; j++){
                        if(cnt % 2 == 0){
                            if(chess[i][j] != 'B') minB++;
                        }else{
                            if(chess[i][j] != 'W') minB++;
                        }
                        cnt++;
                    }
                    cnt--;
                }

                int temp = Math.min(minW,minB);
                min = Math.min(min,temp);
            }
        }

        System.out.println(min);
    }
}

가능한 모든 경우의 수를 구하면 된다.

체스판에서 가능한 모든 8*8 배열을 W로 시작하는 경우와 B로 시작하는 경우를 구하는 방식으로 해결했다.

profile
💻 개발 공부 기록장

0개의 댓글

관련 채용 정보